PDA

View Full Version : Selecting a cell in a given Range Not working



James36
02-19-2024, 09:19 AM
Hi Everyone I am new to this Forum and XL,VBA

But here I'm sending this code
I want to select only 1 cell in a given Range
But it doesn't seem to work .


Dim rng As Range
Set rng = Sheet3.Range("B1:B12")
If ActiveCell = rng Then
MsgBox "Right Cell Selected", vbOKOnly
Else:
MsgBox "Please Select a cell in Ranges", vbOKOnly
End If
Exit Sub

Help Please thanks

Aflatoon
02-19-2024, 10:24 AM
Your code is currently trying to compare the value of the active cell with an array of values. You should use something like Intersect to see if a range is within another range:


Dim rng As Range
Set rng = Range("B1:B12")
If Not Intersect(ActiveCell, rng) is nothing then
MsgBox "Right Cell Selected", vbOKOnly
Else
MsgBox "Please Select a cell in Ranges", vbOKOnly
End If
Exit Sub

James36
02-19-2024, 01:27 PM
Thank You Yes it works ..