VBA & Macro Problems

Apply the Same Table Style to Every Excel Worksheet with VBA

Camila MilosovichCamila Milosovich Dec 2, 2026 868 views

Question details

Apply a consistent Excel table style to data across all worksheets using VBA.

Apply the Same Table Style to Every Excel Worksheet with 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.
Before you start

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).

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open the workbook and select Developer > Visual Basic. If the Developer tab is hidden, enable it in Excel's ribbon settings.

2
Insert a module

In the VBA editor, select the workbook's project and choose Insert > Module.

3
Add the macro

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

4
Run the macro

Return to Excel, select Developer > Macros, choose ApplyTableStyleToSheets, and select Run. Save the workbook after checking the results.

Create and style Excel tables with a VBA macro
Check table names and data ranges: New tables receive names such as Table_1 based on their worksheet index. If a name already exists, rename the existing table or change the naming line before running the macro. CurrentRegion may include adjacent unrelated data or stop at blank rows and columns.
Free Microsoft Office alternative

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.

Format spreadsheet data and work with tables in WPS SpreadsheetsOpen and edit Microsoft Excel formats including .xlsxUse a familiar spreadsheet interface for an easier transitionWork with a free, lightweight office suite
microsoft office alternative - wps office

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.