Best way to declare variables for use in all modules?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
785
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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