user429400
user429400

Reputation: 3325

Selection.clear does not unselect selection

I want to clear unselect the current selection whenever the user starts "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.Clear clear unselect the current selection?

Upvotes: 2

Views: 55529

Answers (4)

user12184429
user12184429

Reputation: 9

Try this

Application.CutCopyMode = False

it works for me

best regard

Upvotes: -1

GazzaLDN
GazzaLDN

Reputation: 5

try this instead

Selection.ClearContents

Upvotes: -3

L42
L42

Reputation: 19727

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

if you code it like this:

sh.Cells.Select
Selection.Clear

Then it will probably work.
But it is preferable not to use 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.

Upvotes: 1

sam092
sam092

Reputation: 1335

Alright,

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

therefore, when you use Selection.Clear, nothing happens

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

Upvotes: 5

Related Questions