VBA variable used in multiple places?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have some code that runs when a workbook is open
Code:
Private Sub Workbook_Open()

Const thisfilename As String = "Credit Note Uploader"
Const thisfileversion As String = "v21.1.0"
etc etc
End Sub

This code is passed into a SQL script which interrogates a database to ensure that the user is using the correct version of the file. All ok and works perfectly.

I want to do some other things with these two constants in at least two other modules. At the moment, I am declaring the constants at the start of each module, my concern is if I need to update any of the values, I need to remember to do it in three places.

Is there a way I can declare them once and they get picked up wherever in the VBA they are referenced?

Sorry, I have googled and declaring them publicly seems to be a popular answer except I haven't managed to get it to work.

As always, all help greatly appreciated.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi,
you can place your constants at TOP of a STANDARD module & declare them Public

VBA Code:
Public Const thisfilename As String = "Credit Note Uploader"
Public Const thisfileversion As String = "v21.1.0"

more info here:Declaring constants (VBA)

Dave
 
Upvote 0
Solution
Hi,
you can place your constants at TOP of a STANDARD module & declare them Public

VBA Code:
Public Const thisfilename As String = "Credit Note Uploader"
Public Const thisfileversion As String = "v21.1.0"

more info here:Declaring constants (VBA)

Dave
Thanks for that; so when the workbook is opened, the variables can be "seen" by the code in the Workbook_Open event?
 
Upvote 0
Thanks for that; so when the workbook is opened, the variables can be "seen" by the code in the Workbook_Open event?

Should do just that - you can do a simple check by placing a msgbox in the event to test it

VBA Code:
Private Sub Workbook_Open()


    MsgBox thisfilename & Chr(10) & thisfileversion



End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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