How to set a project-wide variable

EdNerd

Active Member
Joined
May 19, 2011
Messages
460
This feels like a stupid, basic question that I should know. But I can't get it!! I have a named range on Sheet1, cell $D$4. In Workbook_Open, I need to grab the value in this cell and use it to determine a Boolean variable to use in two separate modules (workbook, not worksheet). "If range = 1, Boolean = True". How do I set this up, please?? (XL 365)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If I'm understanding you correctly, I would set aside a sheet for such variables and save your variable there. So in Workbook_Open, you would run the code that reads $D$4, then write the variable you need to some named range in Sheets("Variables"), and use that named range in your other modules.
 
Upvote 0
If I'm understanding you correctly, I would set aside a sheet for such variables and save your variable there. So in Workbook_Open, you would run the code that reads $D$4, then write the variable you need to some named range in Sheets("Variables"), and use that named range in your other modules.
Thank you for the input. I have two questions here:
-- The cell at Sheet1, $D$4, is already a named range with Workbook scope. I'm having trouble referencing it to set my Boolean.
-- How do I declare and set this variable in the Workbook module to be available in any module across the entire workbook?
 
Upvote 0
Now that I'm looking at it again, I guess what I'm confused about why the value needs to be read when the Workbook is opened, since you can read that value at any time with any module. I might be missing something here.

If it's a named range already, then you should be able to reference it within VBA as Sheet("[sheet_name_here]").Range("[named_cell_here]"). If the value in that cell is TRUE or FALSE, Excel tends to treat it as Boolean. No matter where your code is, I would reference that cell for the Boolean value I need.

VBA Code:
Dim named_cell As Range, named_cell_value As Boolean
    Set named_cell As ThisWorkbook.Sheets(1).Range([named_cell_here])
    ' if you're using 1 and 0 instead of TRUE or FALSE
    If named_cell.Value2 = 1 Then
        named_cell_value = True
    Else
        named_cell_value = False
    End If

You could set the value of that cell through VBA if you need to change it.

VBA Code:
named_cell.Value2 = 0

Then you can read the value again whenever you think you might need an updated value.

Or if you're just trying to do a Workbook named reference without using Sheets at all, I think you can do it this way:
VBA Code:
Range("[Your_sheet_name].xls![named_cell")
 
Upvote 0
In ThisWorkbook module, I have
VBA Code:
'
Public DoDebug As Boolean
'

Sub Workbook_Open()

Dim bolVar As Variant
bolVar = ThisWorkbook.Worksheets("Sheet1").Range(["IEdebug"]).Value

If bolVar = 1 Then
  DoDebug = True
Else
  DoDebug = False
End If

Debug.Print bolVar
Debug.Print DoDebug

bolVar and DoDebug set correctly, according to the value in the cell named "IEdebug".

Clicking in a specific cell in Sheet1 starts the code running in Module 1, which occasionally calls to a sub in Module 2.
In that sub, I have
Code:
 If DoDebug = False Then Exit Sub
The Boolean DoDebug does not exist in that sub.
 
Upvote 0
How do I declare and set this variable in the Workbook module to be available in any module across the entire workbook?
Declare the variable as Public in a normal module. If you declare it in ThisWorkbook you always have to refer to it as ThisWorkbook.DoDebug, not just DoDebug.
 
Upvote 0
Solution

Forum statistics

Threads
1,217,382
Messages
6,136,243
Members
450,001
Latest member
KWeekley08

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