Macro Variables

RichardU

Board Regular
Joined
Aug 3, 2007
Messages
98
I have a variable i want to use through-out my workbook, but Excel does not seem to let me i have defined it etc

Code:
Sub Auto_Open()
Dim ImprestProtected As Integer
ImprestProtected = 1
End Sub

and

Code:
Private Sub Worksheet_Activate()

If ImprestProtected = 0 Then Exit Sub
UserForm12.Show

End Sub

you get the idea.

How can i do this?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
use

Public ImprestProtected As Integer
place it above macro
 
Upvote 0
You need to delcare the variable outside of a procedure (i.e. before any other code). Then it will be a "public" variable and can be used across other procedures.

There is further information under "Understanding Scope and Visibility" in the VBA help files.
 
Upvote 0
Ok i understand now so what does the line

Option Explicit

mean in the code below?


Code:
Option Explicit
Public SignatureList As String
 
Upvote 0
Ok i understand now so what does the line

Option Explicit

mean in the code below?


Code:
Option Explicit
Public SignatureList As String
 
Upvote 0
Option Explicit means that you have to declare (Dim) all your variables. This is a very good idea IMHO as it avoids errors with typos.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,030
Members
448,940
Latest member
mdusw

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