PDA

View Full Version : Solved: Check if aRange = anotherRange



Adamski
01-09-2010, 11:48 AM
How do I check if one range is exactly the same as another?

This didn't work:
if aRange = anotherRange then

So I've been using:
if aRange.address = anotherRange.address then

Is that correct?

xld
01-09-2010, 12:46 PM
It's one way, but be careful.

This



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


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



MsgBox aRange.Address(, , , True) = anotherRange.Address(, , , True)


is better.

Adamski
01-09-2010, 02:30 PM
OK, I see.

Cheers

mikerickson
01-09-2010, 05:48 PM
This returns
False
True
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

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