Amnesia of global object variables

nigelandrewfoster

Well-known Member
Joined
May 27, 2009
Messages
747
Is there any way of declaring and setting the value of a global object variable so it keeps its value when the procedure ends? The system I'm developing runs separate macros according to the user's clicking on clip art, returning the user to Excel level after each one has run. As it stands, the moment the macro finishes, all the global object variables I have declared are cleared, so the next click generates an "Object variable or With block not set" error. I have gotten around this by writing a macro which simply contains SET statements for them all, and each macro then starts with a CALL command to this macro. Is that the correct way to go, or is there a more direct way, please? As ever, I appreciate your time!
Nigel
 
I was doing it to enable to macros to run without them stopping with Object variable etc not set error. Now inclined to think I'm causing this problem as I'm testing them. I'm in new territory here: A user could potentially run one of five macros at this point, each one shares information with the others, so relying heavily on global variables.
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
As a general rule, you should try and pass variables directly between procedures rather than relying on globals, though there are times when you have to use them.
 
Upvote 0
Rather than:
Code:
Public rng as range
Sub Macro1()
set rng = Range("A1")
call macro2
End Sub
Sub Macro2()
rng.value = "Hello"
End Sub

you do:
Code:
Sub Macro1()
Dim rng as Range
set rng = Range("A1")
call macro2(rng)
End Sub
Sub Macro2(rng as Range)
rng.value = "Hello"
End Sub
 
Upvote 0
Thanks for that. Simple, no doubt, but I am very slow picking up VBA concepts, since I still think too much in terms of BASIC. Appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top