Use a VBA Macro to List Excel Worksheets Ending in Data
Question details
List only worksheets whose names end with a space followed by “data.”

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants a VBA macro to list matching worksheets, such as “2016 data” and “2015 data,” instead of every worksheet in a workbook.
- Observed behavior
- The desired list contains only worksheet names with the exact suffix “ data.”
Open the workbook and choose an empty cell with enough blank rows below it for the results. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to keep the macro.
Filter worksheet names with the VBA Right function
Use this macro to write matching sheet names below the selected cell.
Right$(ws.Name, 5) extracts the final five characters, including the space before “data.” StrComp with vbBinaryCompare makes the suffix comparison case-sensitive. VBA does not have a built-in Contains function.
In Excel, open the Developer tab and select Visual Basic. If the Developer tab is hidden, enable it under File > Options > Customize Ribbon.
In the VBA editor, select Insert > Module and paste: Sub ListDataSheets() Dim ws As Worksheet Dim startCell As Range Dim counter As Long Set startCell = ActiveCell counter = 0 For Each ws In ActiveWorkbook.Worksheets If StrComp(Right$(ws.Name, 5), " data", vbBinaryCompare) = 0 Then startCell.Offset(counter, 0).Value = ws.Name counter = counter + 1 End If Next ws End Sub
Return to Excel, activate the workbook to inspect, and click an empty cell where the list should begin.
Select Developer > Macros, choose ListDataSheets, and click Run. Matching names appear in the selected cell and the cells below it.
List matching worksheet names in WPS Spreadsheets
In a WPS Office installation with VBA macro support available and enabled, use the same macro to list worksheet names ending in “ data.” VBA availability may depend on your WPS edition and installation.
- 1. Check macro support: Open the workbook in WPS Spreadsheets and check the Developer tab for the VBA editor. VBA support may require a compatible edition or additional installation.
- 2. Add the macro: Open the VBA editor from the Developer tab, select Insert > Module, and paste the ListDataSheets macro shown above.
- 3. Choose an output cell: Return to the workbook and click an empty cell with blank rows below it.
- 4. Run and save: Use Developer > Macros to run ListDataSheets, then save the workbook in .xlsm format to retain the macro.

Frequently Asked Questions
How do I include names ending in “ Data” as well?
In the VBA editor, replace vbBinaryCompare with vbTextCompare in the StrComp call. The comparison will then ignore letter case.
Why does the macro check five characters instead of four?
The suffix “ data” contains five characters: one space and the four letters in “data.” To match names ending in “data” without requiring a space, use Right$(ws.Name, 4) and compare it with “data.”
Where does the macro put the worksheet names?
It starts at the cell selected before you run Developer > Macros > ListDataSheets and writes each matching name in the next row. Existing values in those cells are overwritten.
Which workbook does the macro search?
It searches ActiveWorkbook.Worksheets, so activate the workbook you want to inspect before selecting the output cell and running the macro.




