Lee Kitson
Lee Kitson

Reputation: 1

Select certain Columns from multiple selected cells

This is my first post here. I have searched and searched for something that can help me but I'm a bit stuck.

What I'm trying to do is change the cell colour of cells in columns B:AI in rows where there are cells selected. Example: If I select B15 & B16, and run the macro, would like cells B15:AI16 colouring a certain colour.

I have tried the below but it only works for the first cell i select.

Sub testMacro()

     For Each cell In Selection
        Range("B" & Selection.Row & ":AI" & Selection.Row).Interior.ColorIndex = 33
     Next cell

End Sub

Can anyone help?

Upvotes: 0

Views: 72

Answers (1)

SJR
SJR

Reputation: 23081

Try this. You need to refer to your cell variable which is the one that changes in each iteration of the loop.

Sub testMacro()

     For Each cell In Selection
        Range("B" & cell.Row & ":AI" & cell.Row).Interior.ColorIndex = 33
     Next cell

End Sub

Upvotes: 2

Related Questions