Holding a variable in a dormant macro

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
15,507
Office Version
  1. 365
Platform
  1. Windows
Not sure if my title is misleading but didn't know how to refer to this problem.

I need to hold a variable (a) in a macro,

this needs the value of a = 0 when the workbook is opened

but needs to be changed to a=1 after the first run of the macro and retain this value for as long as the workbook remains open.

I'm guessing I'm going to feel rather stupid when someone gives me an answer that should have been obvious.:confused:

Thanks in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Use a Global variable like

Code:
Public a As Long
 
Sub FirstMacro()
MsgBox a
a = 1
SecondMacro
End Sub
 
Sub SecondMacro()
MsgBox a
End Sub
 
Upvote 0
how about placing the number 0 somewhere in the excel sheet and designating say

a=activesheet.range(A6).value

then at the end of the macro have

.range(a6).value=1
 
Last edited:
Upvote 0
Thanks Jindon, that makes sense.

In theory then that would mean if i used
Code:
Sub test()
If a > 0 Then do something
more code
a = 1
End Sub

That in the first run a would have a default of 0 because it hasn't been defined.
But after it's defined as a=1 it will remain that way?
 
Upvote 0
Thanks Jindon, that makes sense.

In theory then that would mean if i used
Code:
Sub test()
If a > 0 Then do something
more code
a = 1
End Sub

That in the first run a would have a default of 0 because it hasn't been defined.
But after it's defined as a=1 it will remain that way?

Interpretation of above code is, no execution of test subroutine only for the 1st time when a is delcared as a global variable.
 
Last edited:
Upvote 0
Good answer though Scott, and something I hadn't thought of.

Only problem would be that when the workbook is saved so will the value of 1, which needs to be back to 0 when the workbook is opened again.

how about placing the number 0 somewhere in the excel sheet and designating say

a=activesheet.range(A6).value

then at the end of the macro have

.range(a6).value=1
 
Upvote 0
Thanks Jindon, that's exactly what I wanted to hear :)

Thanks very much for your help.

Interpretation of above code is, no execution of test subroutine only for the 1st time when a is delcared as a global bariable.
 
Upvote 0
In that case, I would use Boolean type variable. (True/False)
False is a default value
Code:
Public PubFlg As Boolean
 
Sub test()
If PubFlg Then do something
more code
PubFlg = True
End Sub
 
Upvote 0
That looks like a better option Jindon,

I'm assuming that default would be Pubflag = False

In the code would "If PubFlg Then" Mean "If Pubflg = True" or "If Pubflg = False"?

Sorry for so many questions but not used this before.

Thanks again.
 
Upvote 0
You could use a static variable
Code:
Sub myRoutine()
    Static hasRun As Boolean

    If hasRun Then
        Rem do something
    Else
        hasRun = True
        Rem something else (first pass only)
    End If

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,905
Messages
6,168,949
Members
452,227
Latest member
sam1121

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