Static sub not preserving variables between calls

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
I don't understand why this is happening. This sub calls a function if a certain Boolean variable is "Not" true. The problem is that this variable is always Empty when the procedure is called. This makes no sense to me.

Code:
Public Static Sub caller()
 
If Not theVariable Then Populate
 
theVariable = True
 
End Sub

The idea here is that the function runs once, and then doesn't run again unless the workbook is closed and reopened.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Glory,

I hope this helps.

If you declare theVariable as Public, then the variable will hold the True False value while the workbook is open, and is available to all your other macros.


Rich (BB code):
Public theVariable As Boolean

Public Static Sub caller()

If Not theVariable = True Then
  theVariable = True
End If
 
End Sub
 
Last edited:
Upvote 0
I actually already did and forgot to mention that.

It's really weird that this is not working. The variable is uninitialized every time the code runs, though. It's always empty until I explicitly let it be True.
 
Upvote 0
Uh... I take it back, I guess? For some reason tonight it's working. I mean, I literally changed nothing, I just opened the workbook I had been toying with last night and ran the code and it started working.

Thanks anyway.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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