Apply the Same Table Style to Every Excel Worksheet with VBA
Question details
Apply a consistent Excel table style to data across all worksheets using VBA.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A workbook contains data on multiple worksheets that should use the same table formatting.
- Observed behavior
- Each worksheet's existing tables should use the selected style, and worksheets without tables should have their data formatted as a table where applicable.
Check that each data range you want to convert starts at A1, has headers in its first row, and is separated from unrelated cells by blank rows and columns. Unprotect any worksheets that need changes and save the workbook as an Excel Macro-Enabled Workbook (.xlsm).
Create and style Excel tables with a VBA macro
Use this macro to style existing tables and create a table from the contiguous region at A1 on sheets that have no tables.
The macro applies TableStyleMedium2 to every existing table. On a sheet without a table, it creates one only when A1 is not empty. It does not create additional tables on sheets that already contain tables.
In Excel, open the workbook and select Developer > Visual Basic. If the Developer tab is hidden, enable it in Excel's ribbon settings.
In the VBA editor, select the workbook's project and choose Insert > Module.
Paste this code into the module: Sub ApplyTableStyleToSheets() Dim ws As Worksheet Dim tbl As ListObject Dim dataRange As Range For Each ws In ThisWorkbook.Worksheets If ws.ListObjects.Count = 0 Then If Not IsEmpty(ws.Range("A1").Value) Then Set dataRange = ws.Range("A1").CurrentRegion ws.ListObjects.Add(xlSrcRange, dataRange, , xlYes).Name = "Table_" & ws.Index End If End If For Each tbl In ws.ListObjects tbl.TableStyle = "TableStyleMedium2" Next tbl Next ws End Sub
Return to Excel, select Developer > Macros, choose ApplyTableStyleToSheets, and select Run. Save the workbook after checking the results.

Work with spreadsheet tables in WPS Office
WPS Office is a free, lightweight alternative for opening and editing spreadsheets in a familiar interface. It supports common Excel formats for easier migration, but it does not fix Excel VBA issues; macro support can vary by version and configuration.

Frequently Asked Questions
How do I use a different Excel table style?
In the VBA editor, replace "TableStyleMedium2" in the TableStyle assignment with another valid Excel table style name, then run the macro again.
Will the macro change tables that already exist?
Yes. It applies the selected style to every existing table on each worksheet. It creates a new table only on a sheet that has no tables and has a nonempty A1 cell.
Why did the macro skip a worksheet?
A worksheet with no tables and an empty A1 cell is skipped. Check the sheet's data location and adjust the A1 reference in the macro if its data starts elsewhere.
What file type should I use to keep the macro?
In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Review your macro security settings and run macros only from trusted sources.




