Keep setting of variables for all macros of a procedure

BBHRVS

Board Regular
Joined
Nov 3, 2005
Messages
94
Hi;

I am using in all the macros of a workbook abbreviations for the worksheets.
Since then, at the beginning of each macro, there was something like :
Set wsh1=Thisworkbook.Worksheets("name1")
Set wsh2=Thisworkbook.Worksheets("name2")
...
I want now to define only once the abbreviations of the worksheets and be able to use them in any macro of the procedure.
I tried with a public sub, but this does not work properly.
What is the correct way to do it ?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi, BBHRVS,

see if this works for you
put this in the workbookmodule
Code:
Private Sub Workbook_Open()
Set wsh1 = ThisWorkbook.Worksheets("name1")
Set wsh2 = ThisWorkbook.Worksheets("name2")
End Sub
and this in a "normal" module
Code:
Public wsh1 As Worksheet
Public wsh2 As Worksheet

Sub test()
wsh1.Select
MsgBox wsh2.Name, 64, "title"
End Sub

kind regards,
Erik
 

BBHRVS

Board Regular
Joined
Nov 3, 2005
Messages
94
Dear Erik;

This is exactly what I did, but the issue is that during macro testing, any interruption, due to bug or voluntary stop, cancels everything which is stored in the variables. Then it begins quite tiring, as you have to reset the wshts ...
 

Watch MrExcel Video

Forum statistics

Threads
1,129,801
Messages
5,638,437
Members
417,025
Latest member
MusterDuster

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
Top