Deactivate a range in Excel 365 VBA

192 views Asked by At

When a range in EXCEL365 is Activated the cells turn grey. They remain grey when the subroutine is completed & exited. I assume that one wants to Deactivate the range before exiting the subroutine to have the cells turn white. This topic is not handled very well in the Microsoft help pages. Any suggestions? The sub gives the correct appearance to the user but does not solve the problem

Sub TestActivate()

Dim myWkBk As Workbookr code here`
Dim myWkSh As Worksheet
Dim myRange As Range
Set myWkBk = ThisWorkbook
Set myWkSh = myWkBk.Worksheets("Sheet1")
Set myRange = Range("A1:L50")
myRange.Activate

Cells(1, 1).Value = 1
Cells(2, 1).Value = 2

Range("A1:L50").Interior.ColorIndex = -4142

End Sub
1

There are 1 answers

0
RetiredGeek On

The best way is to not Activate the range in the first place:

Sub TestActivate()

Dim myWkBk As Workbook
Dim myRange As Range
Set myWkBk = ThisWorkbook
Set myWkSh = myWkBk.Worksheets("Sheet1")
Set myRange = Range("A1:L50")

With myRange
    .Cells(1, 1).Value = 1
    .Cells(2, 1).Value = 2
End With 'myRange

End Sub

HTH