VBA: Pass a sheet to another sub

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I need to loop thru a specified number of worksheets and run a sub. I would like to keep the two subs separately since it actually has two purposes.

Basically, If Sheet5.Range("B1").value = "Prod" then I need to loop thru the 12 sheets, but if not, then I need to just run the Sub RunMe on Sheet3.

I've crafted an initial sub, but not sure how to pass between the two.

Code:
[FONT=Times New Roman][COLOR=#000000]Sub FillEachWs()[/COLOR][/FONT]
[COLOR=#000000][FONT=Times New Roman]    Dim i As Long[/FONT][/COLOR]
[COLOR=#000000][FONT=Times New Roman]    For i = 1 To 12[/FONT][/COLOR]
[COLOR=#000000][FONT=Times New Roman]        If Sheet5.Range("B1").Value ="Prod" Then[/FONT][/COLOR]
[COLOR=#000000][FONT=Times New Roman]            With Sheets(CStr(i))[/FONT][/COLOR]
[COLOR=#000000][FONT=Times New Roman]                Call RunMe[/FONT][/COLOR]
[COLOR=#000000][FONT=Times New Roman]            End With[/FONT][/COLOR]
[COLOR=#000000][FONT=Times New Roman]        Else[/FONT][/COLOR]

[COLOR=#000000][FONT=Times New Roman]    Next i[/FONT][/COLOR]
[FONT=Times New Roman][COLOR=#000000]End Sub[/COLOR][/FONT]
Code:
[FONT=Times New Roman][COLOR=#000000]Sub RunMe()[/COLOR][/FONT]
[COLOR=#000000][FONT=Times New Roman]    Sheet3.Range("AU1").Value ="Check"[/FONT][/COLOR]
[FONT=Times New Roman][COLOR=#000000]End Sub[/COLOR][/FONT]
 
Last edited:
If there are multiple sheets in the workbook, do you still want to put "Check" in cell AU1 on Sheet3, and what do you want to do to the other sheets?

Yes, there will always be a Sheet3 no matter what and it should always get "Check" in AU1. If there are more numbered sheets (1, 2, 3, 4, 5, 6 etc.), those will get "Check" also.
 
Last edited:
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
If you only want it to happen for sheets with a number, try
Code:
Sub FillEachWs()
   Dim Ws As Worksheet
   For Each Ws In ThisWorkbook.Worksheets
      If IsNumeric(Ws.Name) Then Ws.Range("AU1").Value = "Check"
   Next Ws
End Sub
 
Upvote 0
Then Fluff's code in post #8 should work for you.

But that code will act on every sheet. I have a sheet named "Instructions", "Chart", "Control Panel", and about five other sheets I do not need this run on.
 
Upvote 0
Yes, thank you Fluff and Joe. Post #13 did the trick.

Sorry it took so long.
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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