MattB
MattB

Reputation: 15

VBA macro to change color of cells in range when a cell is selected

I'm trying to write code that will, when a user selects a cell within a range, change the color of all the cells in that range. When running the code posted below, i get an error "Object doesn't support this property or method". I noticed that if i edit the code to just paste a "1" in all the cells that it works just fine. What am I doing incorrectly?

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.range)

    If Not Intersect(Target, range("G1:I5")) Is Nothing Then
        For Each cell In range("G1:I5")
        cell.interier.ColorIndex = 10
        Next
    End If
End Sub

Upvotes: 0

Views: 6687

Answers (2)

Gary's Student
Gary's Student

Reputation: 96753

You should be aware that you do not have to loop over the cells:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim r As Range
    Set r = Range("G1:I5")
    If Intersect(r, Target) Is Nothing Then Exit Sub
    r.Interior.ColorIndex = 10
End Sub

Upvotes: 1

mechanical_meat
mechanical_meat

Reputation: 169274

The error is simply a typo of Interior

...
cell.Interior.ColorIndex = 10
'#         ^  
...

Upvotes: 0

Related Questions