Creating a global variable for entire workbook

johannes2008

New Member
Joined
Aug 20, 2010
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Hello All

What I have are a number of macros in "Sheet 1 (Data)" and then 2 macros in a userform. I have declared the variables used in the macros for the user form in "Sheet 1 (Data)" before any macro or sub, however it seems that the values given to those variables in the user form macros are not carried through when I use the same variable in "Sheet 1 (Data)".

Any thoughts on what I am doing wrong?

Thanks for the help in advance
-Johannes
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
...I have declared the variables used in the macros for the user form in "Sheet 1 (Data)" before any macro or sub, however it seems that the values given to those variables in the user form macros are not carried through when I use the same variable in "Sheet 1 (Data)".

Any thoughts on what I am doing wrong?..

Whilst you can assign values to Public variable is a worksheet's module, e.g.:

Userform w/two textboxes and one commandbutton, form code:

Rich (BB code):
Option Explicit
 
Private Sub CommandButton1_Click()
 
    Sheet1.sTextbox1Val = Me.TextBox1.Value
    Sheet1.sTextbox2Val = Me.TextBox2.Value
 
    Unload Me
    Sheet1.ReportVals
End Sub

Sheet1 code:

Rich (BB code):
Option Explicit
 
Public sTextbox1Val As String, sTextbox2Val As String
 
Sub ReportVals()
    MsgBox sTextbox1Val & "  " & sTextbox2Val
End Sub

IMO, probably an unnecessary hassle. Try just declaring the variables as Public in a Standard Module. That way, either Object Module (the sheet's or the form's) can easily reach them.
 
Last edited:
Upvote 0
Hey GTO

Thanks for the reply.

How would I do that? "declaring the variables as Public in a Standard Module" that is? What is the difference between what I am using and a standard module?

Thanks for the help
-Johannes
 
Upvote 0
Greetings Johannes,

In the code window, use either the little icon in the upper-left or the Insert button on the menubar, and pick 'Module' to insert (NOT Class Module). By default, the first one will be named 'Module1'. That is a Standard Module.

Mark
 
Upvote 0
Thanks Mark for your help

one last question (hopefully) how do you declare the variable as public?

Thanks
-Johannes
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

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