Globally Setting Worksheet Variables

scottcolbury

New Member
Joined
Dec 6, 2005
Messages
45
Hi all,

I have a large project with multiple worksheet whose names may change in the future. I'm trying to create worksheet variables in VBA but can't seem to get the dimensioning correct. I'm using 'Option Explicit'.

My code goes something like this...

Code:
Some Sub()
    Call SetSheets(ws1, ws2, ws3)
        With ws1
            Logic code here... 
        end with
End Sub

And then in another module...

Code:
Public ws1 As Worksheet  
Public ws2 As Worksheet  
Public ws3 As Worksheet  

Function SetSheets(ByRef ws1, ByRef ws2, ByRef ws3) 
   Set ws1 = Sheet5
   Set ws2 = Sheet6
   Set ws3 = Sheet13
End Function

However I keep getting a 'duplicate declaration in current scope' error.
Any ideas?

Also, right now I have to call SetSheets() every time I want to use a ws1, ws2, etc variable in a separate procedure. Is there any way to set the ws1, ws2, etc variables at the beginning of my code so that all subsequent procedures will know what worksheets they refer to?

Much appreciated!
s_c
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,

You could try refering to each sheet by it's number. e.g.

Code:
Sheets(3).Select

...would refer to the third sheet in your workbook. This way it doesn't matter what your sheet is called either. However if it is moved from third place to say forth, it would change.

HTH,

Jay
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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