1

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 ?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

ThankYou, brettdj. Are you sure about comma separated declaration. I Have string variables declared as (for example) - Poblic a, b, c, d, As String and they all works ?
@Alegro. He's sure. cpearson.com/excel/DeclaringVariables.aspx Scroll down to "Pay Attention To Variables Declared With One Dim Statement"
Ok, it's clear. Thanks brettdj and Doug. Stack Owerflow is a really nice place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.