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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,079
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
use

Public ImprestProtected As Integer
place it above macro
 
Upvote 0

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
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

RichardU

Board Regular
Joined
Aug 3, 2007
Messages
98
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

RichardU

Board Regular
Joined
Aug 3, 2007
Messages
98
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

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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,191,165
Messages
5,985,039
Members
439,935
Latest member
Monty238

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
Top