Robert Berke
asked on
bigRng.select vs bigRng.activate when bigRng has mutliple cells like [a20,c20,e20]
Today I noticed that when bigRng has multiple cells bigRng.activate and bigRng.select seemed to do the same thing.
That was really weird because I always thought that .select was used to select multiple cells, and that .activate would make ONE of those cells active.
So, my typical code might be
[a1,c1,e1].select
[c1].activate
After some confusion I discovered the .activate logic surpisingly subtle. I believe the following code describes exactly what happens for bigRng.activate
rberke
p.s. I used the following code to convince myself that my interpretation is correct. If anyone disagrees, perhaps they can use similar code to prove me wrong.
Sub test()
Dim rBefore As Variant, anyRng As Variant, correct As String, mine As String
For Each rBefore In Array([b2,c3])
For Each anyRng In Array([a1:b2], [c3:d4], [c1:c5])
Debug.Print "----"
rBefore.Select
anyRng.Activate
correct = Selection.Address & ActiveCell.Address
Debug.Print correct
rBefore.Select
myActivate anyRng
mine = Selection.Address & ActiveCell.Address
Debug.Print mine
If correct <> mine Then
Stop
End If
Next
Next
End Sub
That was really weird because I always thought that .select was used to select multiple cells, and that .activate would make ONE of those cells active.
So, my typical code might be
[a1,c1,e1].select
[c1].activate
After some confusion I discovered the .activate logic surpisingly subtle. I believe the following code describes exactly what happens for bigRng.activate
Option Explicit
Sub myActivate(anyRng)
If Intersect(Selection, anyRng.Cells(1)) Is Nothing Then
' if rng's first cell does NOT overlap current selection, then rng.select and rng.activate are identical
anyRng.Select
Else
' otherwise, if rng's first cell DOES overlap current selection, that cell is activated
anyRng.Select
anyRng.Cells(1).Activate
End If
End Sub
Does anybody disagree with this interpretation? Also, I spent quite a bit of time googling for any documentation of what happens when bigRng has multiple cells, but I never found anything that confirmed or denied my interpretation. I will award extra points to anybody that can find such documentation.rberke
p.s. I used the following code to convince myself that my interpretation is correct. If anyone disagrees, perhaps they can use similar code to prove me wrong.
Sub test()
Dim rBefore As Variant, anyRng As Variant, correct As String, mine As String
For Each rBefore In Array([b2,c3])
For Each anyRng In Array([a1:b2], [c3:d4], [c1:c5])
Debug.Print "----"
rBefore.Select
anyRng.Activate
correct = Selection.Address & ActiveCell.Address
Debug.Print correct
rBefore.Select
myActivate anyRng
mine = Selection.Address & ActiveCell.Address
Debug.Print mine
If correct <> mine Then
Stop
End If
Next
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.