Consulting

Results 1 to 4 of 4

Thread: Solved: Check if aRange = anotherRange

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Solved: Check if aRange = anotherRange

    How do I check if one range is exactly the same as another?

    This didn't work:
    [VBA]if aRange = anotherRange then[/VBA]

    So I've been using:
    [VBA]if aRange.address = anotherRange.address then[/VBA]

    Is that correct?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's one way, but be careful.

    This

    [vba]

    Dim aRange As Range
    Dim anotherRange As Range

    Set aRange = Range("Sheet1!A1:A10")
    Set anotherRange = Range("Sheet2!A1:A10")

    MsgBox aRange.Address = anotherRange.Address
    [/vba]

    shows true, when it isn't. But this,

    [vba]

    MsgBox aRange.Address(, , , True) = anotherRange.Address(, , , True)
    [/vba]

    is better.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    OK, I see.

    Cheers

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This returns
    False
    True
    [VBA]Sub test()
    Dim aRange As Range, bRange As Range
    Set aRange = Range("A1:A10")
    Set bRange = Range(Cells(1, 1), Cells(10, 1))

    MsgBox (aRange Is bRange) & vbCr & (aRange.Address(, , , True) = bRange.Address(, , , True))
    End Sub[/VBA]

    I've always assumed that the False is the result of a glitch in VBA. Is that correct?

Posting Permissions

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