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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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,215,634
Messages
6,125,931
Members
449,274
Latest member
mrcsbenson

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