Run Excel VBA Code for Matching Worksheet Names
Question details
Check whether worksheets in the active workbook match either of two predefined name groups before running a VBA procedure on them.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA procedure should process only worksheets whose names appear in one of two predefined groups.
- Observed behavior
- The procedure should run for each matching worksheet and skip worksheets whose names are not in either group.
Identify the worksheet names in each group and decide what the procedure should do with each matching sheet. Open the workbook you want to process and make sure macros are permitted in your Excel environment.
Match each worksheet against both name groups
Use this approach when both groups should be processed by the same procedure.
The macro captures the active workbook, loops through its worksheets, and uses Application.Match to check each name against both arrays. The example names are plain text; replace them with your own worksheet names.
In Excel, open the VBA editor from Developer > Visual Basic, then choose Insert > Module.
Paste this code into the module and replace the example names with your worksheet names: Option Explicit Sub RunForMatchingSheets() Dim wb As Workbook Dim ws As Worksheet Dim groupOne As Variant Dim groupTwo As Variant Set wb = ActiveWorkbook groupOne = Array("Sales", "Inventory") groupTwo = Array("Summary", "Forecast") For Each ws In wb.Worksheets If Not IsError(Application.Match(ws.Name, groupOne, 0)) _ Or Not IsError(Application.Match(ws.Name, groupTwo, 0)) Then ProcessMatchingSheet ws End If Next ws End Sub Sub ProcessMatchingSheet(ByVal ws As Worksheet) ' Add the actions to perform on ws here. End Sub
In ProcessMatchingSheet, replace the comment with your VBA actions and refer to the passed worksheet as ws. For example, use ws.Range("A1") rather than relying on ActiveSheet.
Make the intended workbook active, then use Developer > Macros to run RunForMatchingSheets. Save the code in a macro-enabled workbook if you need to keep it.

Work with spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing spreadsheets. Its Microsoft Office format compatibility can help you move existing files, but it does not fix Excel VBA issues, and macro compatibility may vary.

Frequently Asked Questions
How do I add more worksheet names to a group?
Add quoted names to the relevant Array line in the VBA module, separated by commas. For example, use Array("Sales", "Inventory", "Orders").
Can I process the two groups differently?
Yes. In the For Each loop, use separate If or ElseIf checks for groupOne and groupTwo, then call the appropriate procedure for each match. Decide how to handle a name that appears in both groups.
Does the worksheet name match depend on capitalization?
No. Application.Match with a match type of 0 is not case-sensitive. Use plain worksheet names in the arrays; wildcard characters in lookup values can affect matching.
What happens if none of the worksheet names match?
The loop finishes without calling ProcessMatchingSheet. You can add a counter and display a message after the loop if you want a notification.
How do I keep the VBA code in my workbook?
Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). When reopening the file, enable macros only if you trust its source.




