O
Otto Moehrbach
Excel 2002, WinXP
It seems to me this is simple but somehow I have gotten it twisted in my
mind.
Say that I have a range set, MyRng, in sheet "One".
Now I want to call another macro and pass MyRng in the call.
But at the time of this call, the active sheet is sheet "Two".
If I say:
Call Test2(Range(MyRng.Address))
it seems to me that I will pass a range with the same address but from sheet
"Two", whereas I want to pass the range that I originally set in sheet "One"
I tested this and that is exactly what happens. I used the following 2
macros for this test. In A1 of each sheet I typed the sheet name.
Sub Test1() 'Sheet "One" is active
Dim MyRng As Range
Set MyRng = Range("A1:A5")
MsgBox "Test1" & " " & MyRng(1).Value 'Shows "One"
Sheets("Two").Activate
Call Test2(Range(MyRng.Address))
End Sub
Sub Test2(TheRng As Range)
MsgBox "Test2" & " " & TheRng(1).Value 'Shows "Two"
End Sub
I realize that I can simply call the second macro while the first sheet is
active, but in reality, the first sheet is never active and I set that range
with a With/End With construct.
How can I pass a range in a macro call without running into this problem?
Thanks for your help. Otto
It seems to me this is simple but somehow I have gotten it twisted in my
mind.
Say that I have a range set, MyRng, in sheet "One".
Now I want to call another macro and pass MyRng in the call.
But at the time of this call, the active sheet is sheet "Two".
If I say:
Call Test2(Range(MyRng.Address))
it seems to me that I will pass a range with the same address but from sheet
"Two", whereas I want to pass the range that I originally set in sheet "One"
I tested this and that is exactly what happens. I used the following 2
macros for this test. In A1 of each sheet I typed the sheet name.
Sub Test1() 'Sheet "One" is active
Dim MyRng As Range
Set MyRng = Range("A1:A5")
MsgBox "Test1" & " " & MyRng(1).Value 'Shows "One"
Sheets("Two").Activate
Call Test2(Range(MyRng.Address))
End Sub
Sub Test2(TheRng As Range)
MsgBox "Test2" & " " & TheRng(1).Value 'Shows "Two"
End Sub
I realize that I can simply call the second macro while the first sheet is
active, but in reality, the first sheet is never active and I set that range
with a With/End With construct.
How can I pass a range in a macro call without running into this problem?
Thanks for your help. Otto