Making a sub to set names for worksheets and use that sub in other subs.

Hey Experts,

I was wondering i've this is possible. I want to make a sub like this you see below you.
Option Explicit
Sub Werkbladen()
Dim wsf As Worksheet
Dim Wsa As Worksheet
Dim Wsk As Worksheet
Dim Wsp As Worksheet
Dim Wsv As Worksheet
Dim Wsd As Worksheet

Set wsf = Worksheets("factuur")
Set Wsa = Worksheets("attest")
Set Wsk = Worksheets("klantenbestand")
Set Wsp = Worksheets("productenbestand")
Set Wsv = Worksheets("vaste waardes")
Set Wsd = Worksheets("database")
End Sub

Open in new window

And then use the name "Werkbladen" in other subs like this.
Option Explicit

Private Sub test()
Werkbladen
wsf.Range("A1").Value = 5
End Sub

Open in new window

But then i get variable not defined. Is it even possible what i am trying to do. Or should you always dim and set al the worksheets for every sub you make?

Thank you for your help!
omlaagAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
I don't see why you would want to do that, proper design would be to declare and set in each sub-procedure any worksheets you would need.
0
KimputerCommented:
Aside from proper design (probably need full code to design it), try this as your full code:

Option Explicit

Dim wsf As Worksheet
Dim Wsa As Worksheet
Dim Wsk As Worksheet
Dim Wsp As Worksheet
Dim Wsv As Worksheet
Dim Wsd As Worksheet

Sub Werkbladen()

	Set wsf = Worksheets("factuur")
	Set Wsa = Worksheets("attest")
	Set Wsk = Worksheets("klantenbestand")
	Set Wsp = Worksheets("productenbestand")
	Set Wsv = Worksheets("vaste waardes")
	Set Wsd = Worksheets("database")
	
End Sub


Private Sub test()

	Werkbladen
	wsf.Range("A1").Value = 5

End Sub

Open in new window

0
MacroShadowCommented:
On a side note, if those worksheets are in the active workbook, you don't have to assign them to variables, any one of the following will work:

Using the index of the worksheet (1 based index)
ActiveWorkbook.Sheets(1).Range("A1").Value = 5

Using the name of the worksheet
ActiveWorkbook.Sheets("factuur").Range("A1").Value = 5
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

omlaagAuthor Commented:
Hopefully i explain it better this way, for this application i have like 10 forms and then i got some modules. But now for every sub in every form and module i have typed to set and dim the worksheets. So i want one sub where all the worksheets are set. So that in  all the rest of the forms and modules i only have to type "Werkbladen" to set and dim al the worksheets.
This saves time and changes to make an error but maybe it isn't possible or not the way to program but i thought that it could save some time.

The solution from kimputer the code have to be in de same module or form.
0
MacroShadowCommented:
In the solution from kimputer just put the code into a new module, in lines 3-8 change the word "Dim" to "Public".

You only have to call the Werkbladen sub-routine once.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
omlaagAuthor Commented:
Thank you for the help this is what i needed!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.