The default property of a range object is .Value
so when you say If Target = rTime.Offset(0, 1)
, it will always compare the values in that range rather than the address of those ranges.
One way is already shown by L42. Here is another way using Intersect
If Not Intersect(Target, rtime.Offset(0, 1)) Is Nothing Then cboStatus.Activate
EDIT
When you say Target.Column
and Target.Row
, you will always get the first column and the first row of the cell in that range, even if Target
has multiple cells. To avoid this use the below to ensure that you have the desired Target
. Your code will give you unexpected results even if there is a single cell in Target
. For example, say the value of cell B1
is equal to any other cell which at the moment is target
. So if Cell B1
= "Sid" and Cell F1
= "Sid" and you select cell F1
then you will get the "Hello World" message box.
For xl2003, you can use an additional check
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rtime As Range
Set rtime = Range("A1")
If Target.Cells.Count > 1 Then
MsgBox "you have chosen more than one cell"
Exit Sub
End If
If Not Intersect(Target, rtime.Offset(0, 1)) Is Nothing Then
MsgBox "Hello World"
End If
End Sub
For xl2007+, replace Target.Cells.Count
with Target.Cells.CountLarge
For L42
Your method is correct but then you also will have to put the above check to get the correct results.