vba 编程Excel时将选择设置为Nothing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/292779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:29:13  来源:igfitidea点击:

Setting selection to Nothing when programming Excel

excelvbaexcel-vba

提问by Curt

When I create a graph after using range.copy and range.paste it leaves the paste range selected, and then when I create a graph a few lines later, it uses the selection as the first series in the plot. I can delete the series, but is there a more elegant way to do this? I tried

当我在使用 range.copy 和 range.paste 之后创建一个图形时,它会保留选择的粘贴范围,然后当我在几行之后创建一个图形时,它使用该选择作为图中的第一个系列。我可以删除该系列,但有没有更优雅的方法来做到这一点?我试过

Set selection = nothing

but it won't let me set selection. I also tried selection.clear, but that just cleared the last cells that were selected, and still added an extra series to the plot.

但它不会让我设置选择。我也尝试了 selection.clear,但这只是清除了最后选择的单元格,并且仍然向图中添加了一个额外的系列。

采纳答案by shahkalpesh

Cells(1,1).Select

It will take you to cell A1, thereby canceling your existing selection.

它将带您到单元格 A1,从而取消您现有的选择。

回答by David

Application.CutCopyMode = False

回答by user3032537

Select any cell and turn off cutcopymode.

选择任何单元格并关闭 cutcopymode。

Range("A1").Select
Application.CutCopyMode = False

回答by TheAxeman

Just use

只需使用

SendKeys "{ESC}"

发送键“{ESC}”

thereby cancelling your selection.

从而取消您的选择。

回答by Miguel

There is a way to SELECT NOTHING that solve your problem.

有一种方法可以 SELECT NOTHING 解决您的问题。

  1. Create a shape (one rectangle)
  2. Name it in Shapes Database >>> for example: "Ready"
  3. Refer to it MYDOC.Shapes("Ready") and change the visibility to False
  1. 创建一个形状(一个矩形)
  2. 在 Shapes Database >>> 中命名,例如:“Ready”
  3. 参考它 MYDOC.Shapes("Ready") 并将可见性更改为 False

When you want that Excel SELECT NOTHING do it:

当您想要 Excel SELECT NOTHING 时,请执行以下操作:

MYDOC.Shapes("Ready").visible=True
MYDOC.Shapes("Ready").Select
MYDOC.Shapes("Ready").visible=False

This HIDE the selection and nothing still selected in your window PLUS: The word "Ready" is shown at the Left Top in your Sheet.

这隐藏了选择,并且在您的窗口中仍然没有选择 PLUS:“准备好”一词显示在您的工作表的左上角。

回答by RHH1095

You can simply use this code at the end. (Do not use False)

您可以简单地在最后使用此代码。(不要使用 False)

Application.CutCopyMode = True

回答by Fionnuala

I do not think that this can be done. Here is some code copied with no modifications from Chip Pearson's site: http://www.cpearson.com/excel/UnSelect.aspx.

我不认为这是可以做到的。以下是从 Chip Pearson 的站点复制的一些代码,未经修改:http: //www.cpearson.com/excel/UnSelect.aspx

UnSelectActiveCell

取消选择活动单元格

This procedure will remove the Active Cell from the Selection.

此过程将从选择中删除活动单元格。

Sub UnSelectActiveCell()
    Dim R As Range
    Dim RR As Range
    For Each R In Selection.Cells
        If StrComp(R.Address, ActiveCell.Address, vbBinaryCompare) <> 0 Then
            If RR Is Nothing Then
                Set RR = R
            Else
                Set RR = Application.Union(RR, R)
            End If
        End If
    Next R
    If Not RR Is Nothing Then
        RR.Select
    End If
End Sub

UnSelectCurrentArea

取消选择当前区域

This procedure will remove the Area containing the Active Cell from the Selection.

此过程将从选择中删除包含活动单元格的区域。

Sub UnSelectCurrentArea()
    Dim Area As Range
    Dim RR As Range

    For Each Area In Selection.Areas
        If Application.Intersect(Area, ActiveCell) Is Nothing Then
            If RR Is Nothing Then
                Set RR = Area
            Else
                Set RR = Application.Union(RR, Area)
            End If
        End If
    Next Area
    If Not RR Is Nothing Then
        RR.Select
    End If
End Sub

回答by Madhur Kashyap

In Excel 2007, a combination using select and CutCopyMode property, it is possible to reset all the selections. It worked for my use case.

在 Excel 2007 中,结合使用 select 和 CutCopyMode 属性,可以重置所有选择。它适用于我的用例。

Application.CutCopyMode = xlCopy
ActiveSheet.Range("A" & lngRow).Select

Regards Madhur

问候马杜尔

回答by JustinMT

I had this issue with Excel 2013. I had "freeze panes" set, which caused the problem. The issue was resolved when I removed the frozen panes.

我在 Excel 2013 中遇到了这个问题。我设置了“冻结窗格”,这导致了这个问题。当我移除冻结的窗格时,问题得到解决。

回答by BOBY

Sub MyFunc()

    Range("B6").Select

    Selection.Locked = True

End Sub