Consulting

Results 1 to 3 of 3

Thread: Selecting a cell in a given Range Not working

  1. #1
    VBAX Newbie
    Joined
    Feb 2024
    Posts
    2
    Location

    Selecting a cell in a given Range Not working

    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

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    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
    Be as you wish to seem

  3. #3
    VBAX Newbie
    Joined
    Feb 2024
    Posts
    2
    Location
    Thank You Yes it works ..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •