Home » excel » excel vba – How to Public Set – Public Declared variables?

excel vba – How to Public Set – Public Declared variables?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a separate module to declare public variables

Public ws1, ws2 As Worksheet

On each module, on each procedure I must repeat the following :

Set ws1 = Sheets("Sun")
Set ws2 = Sheets("Moon")

…and then – write the code.

So, WHERE and HOW can I Set this variables so they are allready set on each module, on each Sub or Function – as Sheets Sun and Moon ?

How to&Answers:

Once you have it set it will remain available to the other modules. So I suggest you set it on Workbook_Open. Also your current code does not dimension ws1 as a worksheet, so I suggest you use

Standard Module

Public ws1 As Worksheet, ws2 As Worksheet

ThisWorkbook Module

Private Sub Workbook_Open()
Set ws1 = ThisWorkbook.Sheets("Sun")
End Sub