Loop thru sheets, and run same code for sheet(s) based on InputBox value

craigexcel

Active Member
Joined
Jun 28, 2006
Messages
298
Office Version
  1. 2016
Platform
  1. Windows
I'm using Office 365 Excel, with Windows 7 Enterprise.
I have multiple workbooks, that all have 12 monthly worksheets, plus several other sheets. I want to make certain updates in a given workbook, based on the value entered in the InputBox -- 1 for the "DEC18" sheet only, and, 2 for all 12 monthly sheets. (If there's a better way to do this, or any other part of the code for that matter, I'd appreciate your input!)

As I'm processing data in the December workbooks, I want to enter either a 1 or a 2 in the InputBox, and then for the code to either update only the "DEC18" worksheet, or, update all the monthly sheets, while ignoring any sheets that are not the 'monthly' sheets. The same code (which works for a single sheet as it is written now) needs to be run, but only for the sheet(s) 'identified' by having entered either a 1 or 2.

I can't wrap my head around what combo of For Each, Do ___, If, etc. would work best to do this.

So far, I have written this snippet:
Code:
'''''sAns = InputBox("Do you want to make ROUND formula updates in ""DEC18"" sheet ONLY, or, ALL PII sheets?" & _
'''''        vbNewLine & vbNewLine & _
'''''        "Enter 1 for DEC18 sheet ONLY" & _
'''''        vbNewLine & _
'''''        "Enter 2 for ALL PII sheets", _
'''''        "Update which sheets?", 1)
'''''
'''''If sAns = "" Then   'if 'Cancel' button pressed, Exit Sub
'''''    Exit Sub
'''''Else
'''''End If
'''''
'''''If ws.Name = "DEC18" And sAns = 1 Then
'''''For Each ws In Worksheets   'includes any hidden sheets
'''''    If ws.Name Like "[A-Z][A-Z][A-Z]##" Then    'if sheet name matches the pattern [AAA##], i.e 3 UPPER CASE alpha, followed by 2 numeric

Any thoughts that would get me pointed in a different (better!) direction? Thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Maybe something like this...
Code:
If sAns = 1 Then
For Each ws In ThisWorkbook.Worksheets   'includes any hidden sheets
If ws.Name = "DEC18" Then    'if sheet name matches the pattern [AAA##], i.e 3 UPPER CASE alpha, followed by 2 numeric
'do stuff for Dec 18 sheet
exit sub
End If
Next ws
Else
For Each ws In ThisWorkbook.Worksheets   'includes any hidden sheets
If ws.Name <> "DEC18" Then    'if sheet name matches the pattern [AAA##], i.e 3 UPPER CASE alpha, followed by 2 numeric
'do stuff for other sheets in wb
End If
Next ws
End If
HTH. Dave
 
Last edited:
Upvote 0
Many thanks, Dave, for your suggestion. I added a CALL to the code that needs to be run for the worksheets, and it works fine. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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