How to set multiple columns in "If Target.Column ="

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to run the same code when one of several columns contain the active
cell. How do I do this without repeating the code.
When I used

If Target.Column = 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 13 Then

the code is executed when any cell in the sheet is selected not just in the
above columns.
I tried using an array without luck either.
Also, I am not sure yet if all of the columns will be sequential or not.
Thanks in advance for any assistance.
Brad
 
Brad,

You can use the Intersect() function

Private Sub Worksheet_Change(ByVal Target As Range)
'if using the change event
If Not Intersect(Target, Range("G:M")) Is Nothing Then _
MsgBox MyCell.Address
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'if using the selection change event
If Not Intersect(ActiveCell, Range("G:M")) Is Nothing Then _
MsgBox MyCell.Address
End Sub

Regards,
KL
 
const START_COL as integer=7
const END_COL as integer=13

If Target.Column >= START_COL and Target.Column <= END_COL Then
'do stuff
end if


Tim
 
Back
Top