vba Selection.clear 不会取消选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19675426/
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 17:04:00  来源:igfitidea点击:

Selection.clear does not unselect selection

excelvbaselection

提问by user429400

I want to clearunselect the current selection whenever the user starts "MyMacro".

每当用户启动“MyMacro”时,我想清除取消选择当前选择。

I'm using the following code:

我正在使用以下代码:

Sub MyMacro()

    If Not IsSheetExists("Memory") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).name = "Memory"
    End If

    Sheets("Memory").Visible = xlSheetVisible 'change to xlSheetVeryHidden
    ActiveWorkbook.Sheets("Sheet1").Activate

    ClearAllSheets

    '......
End Sub

Sub ClearAllSheets()
    For Each sh In ActiveWorkbook.Sheets
        sh.Cells.Clear
        sh.Buttons.Delete
        Selection.Clear
    Next sh
End Sub

Why doesn't Selection.Clearclearunselect the current selection?

为什么不Selection.Clear清除取消选择当前选择?

回答by sam092

Alright,

好吧,

.Clearis meant to clear the contents (& formatting) inside a cell/range, but not unselect

.Clear旨在清除单元格/范围内的内容(和格式),但不能取消选择

therefore, when you use Selection.Clear, nothing happens

因此,当您使用时Selection.Clear,没有任何反应

As you have mentioned also, why not just select a dummy cell?

正如您也提到的,为什么不选择一个虚拟单元格?

回答by L42

Selection.Cleardoesn't work since you don't have any selected range.

Selection.Clear不起作用,因为您没有任何选定的范围。

if you code it like this:

如果你这样编码:

sh.Cells.Select
Selection.Clear

Then it will probably work.
But it is preferable not to use Select.

那么它可能会起作用。
但最好不要使用Select.

Update:add this:

更新:添加这个:

Dim rng as range

Set rng = Selection

or in your case:

或者在你的情况下:

set rng = sh.Application.Selection

then execute:

然后执行:

Selection.Clear.

it will clear the currently selected range.

它将清除当前选择的范围。

回答by user12184429

Try this

尝试这个

Application.CutCopyMode = False

it works for me

这个对我有用

best regard

最良好的问候

回答by GazzaLDN

try this instead

试试这个

Selection.ClearContents