VBA declar & pass worksheet name through subs

ABCDH

New Member
Joined
Sep 30, 2009
Messages
12
Hi - I would like to pass worksheet names through multiple subs, but I'm not sure the best way to do this. I would like to say that w1=Sheet1, w2 = Sheet2 and be able to use w1, w2 through all subs. Below is an example. Thanks!!

Code:
Dim w1 as Worksheet, w2 as Worksheet

Sub RunAllSubs()
Test
Code
Hmmm
End Sub

Sub Test()
w1.visible = true
w2.visible = true
End Test

Sub Code()
w2.UsedRange.Copy Destination:=w1.Columns(1)
End Sub

Sub Hmmm()
RowNumber = w2.cells(Rows.Count,"A").End(xlUp).Row
End Sub
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
Sub RunAllSubs()
    Call Test(w1, w2)
    Call Code(w2)
    Call Hmmm(w2)
End Sub

Sub Test(sheet1, sheet2)
    sheet1.Visible = True
    sheet2.Visible = True
End Sub

Sub Code(sheet)
    sheet.UsedRange.Copy Destination:=w1.Columns(1)
End Sub

Sub Hmmm(sheet)
    RowNumber = sheet.Cells(Rows.Count, "A").End(xlUp).Row
End Sub
 
Upvote 0
I just realized that you did not input w1=Sheet1, w2=Sheet2.

Is this automatically connected with Call Test(w1,w2) and Sub Test(sheet1,sheet2)?

Thank you!
 
Upvote 0
w1 is supposed to be a variable holding sheet. It can be also programmatic name (in properties of sheet there's a "(Name)" property - it's programmatic name).
But if you wish to explicitly reference to them, then this is it (Call says that we're calling some method - we can do without it, of course):

Code:
Sub RunAllSubs()

    Call Test(Sheets(1), Sheets(2))
    Call Code(Sheets(2))
    Call Hmmm(Sheets(2))

    'Or without "Call"
    'Test Sheets(1), Sheets(2)
    'Code Sheets(2)
    'Hmmm Sheets(2)
    
End Sub

Sub Test(sheet1, sheet2)
    sheet1.Visible = True
    sheet2.Visible = True
End Sub

Sub Code(sheet)
    sheet.UsedRange.Copy Destination:=w1.Columns(1)
End Sub

Sub Hmmm(sheet)
    RowNumber = sheet.Cells(Rows.Count, "A").End(xlUp).Row
End Sub
 
Upvote 0
OK. Thank you for the explanation. I will need to play with it to understand how it works. Thank you so much for pointing me in the right direction!
 
Upvote 0

Forum statistics

Threads
1,216,033
Messages
6,128,427
Members
449,450
Latest member
gunars

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