Option Explicit to Declare Variables on Worksheet Code

bisel

Board Regular
Joined
Jan 4, 2010
Messages
223
Office Version
  1. 365
Platform
  1. Windows
Hello All,

When using Option Explicit at the top of a module, is the use restricted to public modules? Or is it supposed to work for modules that are specific a worksheet. For example, if I place this at the top of a Worksheet module ...

VBA Code:
Option Explicit
Public shtprotect As Boolean
Public endprotect As Boolean

Are those variables supposed to then be declared for all subs in that module, even though those subs are Private Sub ... ?

I get variable not declared errors doing this.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Option Explicit only works for the module it's in, so you really need it in every module.
Public Variables declared in a standard module are available to all modules in that workbook, but public variables declared in a sheet module are only directly available to that sheet.
 
Upvote 0
Option Explicit only works for the module it's in, so you really need it in every module.
Public Variables declared in a standard module are available to all modules in that workbook, but public variables declared in a sheet module are only directly available to that sheet.

Thanks for reply. I only want the variable to be available in the sheet. Don't need it anywhere else. Do you see anything wrong with this at the top of the sheet module ...

VBA Code:
Option Explicit
Public shtprotect As Boolean
Public endprotect As Boolean

In this usage, at the top of the sheet module, I get the Variable Not Declared error when I attempt to run the other subs on the sheet module. Those other subs are private subs as they are on the sheet and in a public module ...

Thanks,

Steve
 
Upvote 0
If you only want it available for that sheet I would replace Public with Dim, but I don't think it should make any difference.
However this is confusing, if not contradictory.
Those other subs are private subs as they are on the sheet and in a public module
Are the subs in a sheet module, or a standard module?
 
Upvote 0
If you only want it available for that sheet I would replace Public with Dim, but I don't think it should make any difference.
However this is confusing, if not contradictory.

Are the subs in a sheet module, or a standard module?

I should be more clear on what I am trying to do ...

On the sheet module, I have a sub that runs on sheet activation, Private Sub Worksheet_Activate(). In that sub, I want to set a variable depending on if the sheet is in a protected or unprotected state. If protected, I set the variable called "shtprotect" to TRUE otherwise set it to FALSE.

In another sub on the same sheet module I have a sub called Private Static Sub Worksheet_SelectionChange(ByVal Target As Range). I would like this sub to take an action depending on if the variable, shtprotect is TRUE or FALSE.

I want the variable, shtprotect, to be available to all the subs on the same sheet module. But, I do not want to pass the variable value as one would do in a call routine. I just want to set the variable when the sheet activates and have it available for use.

I think I am probably going about this incorrectly and would appreciate any help. Perhaps I could set a range on the sheet equal to either TRUE or FALSE depending on the state of protection for the sheet. Then use the activate sub to set that range accordingly. Then test the range in subsequent subs. I think that would work, but doesn't sound very elegant.

Steve
 
Upvote 0
For that scenario, what you are doing is fine.
One word of warning, if that sheet is the active when you open the workbook then the variable will be false, which may cause problems with the selectionchange event.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
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