Best way to declare variables for use in all modules?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
As title, I'm unsure of the correct way to do this as I've seen a few ways.

On workbook open ?
Public / Const?

Example:
myVar1 = Sheets("Sheet1").Range("A1").value

How can I use myVar1 in all macros without any further declaration

Any info appreciated
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Declare in a standard module
Code:
Public myVar1 As String
Use the appropriate variable type
 
Last edited:
Upvote 0
Declare in a standard module
Code:
Public myVar1 As String
Use the appropriate variable type

Thanks
So I can create 1 module and name it myVariables

Then store all in here?
Example

Module Name: myVariables
Code:
public myVar1 as integer, myVar2 as integer

myVar1 = 1
myVar2 = 1

Module1
Code:
Private Sub Test1()
Sheets("Data").Range("A1") = myVar2
End Sub
 
Upvote 0
Is it a variable that will change or a constant value?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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