Apply Table Style To Range Of Worksheets

Al Del

Board Regular
Joined
Jan 9, 2007
Messages
112
Would like to apply a table style to a range of worksheets, I get a "Compile error: no sub or function not defined." error when I run this code.

The tables have identical number of columns with varying rows. The all start at A1.

Code:
Sub ChangeStyle()
Dim x As Integer

 For x = 6 To Worksheets.Count
    With Workheets(x)
      .ListObjects.Add(xlSrcRange, .Range("$A$1"), , xlYes).Name = "Table" & x
      .ListObjects("Table" & x).TableStyle = "TableStyleMedium10"

    End With
 Next x

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Thanks, I still get an error, "A table can't ovrelap another table." on line:

Code:
.ListObjects.Add(xlSrcRange, .Range("$A$1"), , xlYes).Name = "Table" & x
 
Upvote 0
Try this:
Code:
Sub Table_Style()
'Modified 6/28/18 7:15 PM EDT
Dim lb As ListObject
Dim i As Long
For i = 6 To Sheets.Count
    For Each lb In Sheets(i).ListObjects
        lb.TableStyle = "TableStyleMedium10"
    Next
Next
End Sub

This assumes you only want to change the Table Style
 
Last edited:
Upvote 0
Worked great, thank you.

Trying to make the procedure more user friendly, by allowing the user to type the table style into cell B20, sheet5 (Merge).

Go from this,
Code:
lb.TableStyle = "TableStyleMedium10"

To something like this (this doesn't work), ideally I would like to only type "Medium10" into B20
and concatenate it to the "TableStyle" statement.
Code:
lb.TableStyle = ThisWorkbook.Sheet5.Range("B20")
 
Upvote 0
Why not try this:
You will get a Inputbox wanting to know the style you want

Code:
Sub Table_Style()
'Modified 6/29/18 12:23 AM EDT
Dim lb As ListObject
On Error GoTo M
Dim i As Long
Dim ans As String
ans = InputBox("Enter style you want", "TableStyle", "TableStyle")
For i = 6 To Sheets.Count
    For Each lb In Sheets(i).ListObjects
        lb.TableStyle = ans
    Next
Next
Exit Sub
M:
MsgBox "Your Table Style of  " & ans & vbNewLine & "Does not Exist"
End Sub
 
Upvote 0
Works good, can the cursor on the message box be move to the end of the TableStyle text, so it doesn't accidentally get deleted.

I also noticed there is no style designation for table style "none" is there something that can be entered to show no table style?
 
Upvote 0
I know of no way to move cursor to end of line.
To clear all table formatting press the Cancel Key.
You will see this note in the Message box Title menu.
Code:
Sub Table_Style()
'Modified 6/29/18 1:05 AM EDT
Dim lb As ListObject
On Error GoTo M
Dim i As Long
Dim ans As String
ans = InputBox("Enter style you want", "To clear table style press Cancel", "TableStyle")
For i = 6 To Sheets.Count
    For Each lb In Sheets(i).ListObjects
        lb.TableStyle = ans
    Next
Next
Exit Sub
M:
MsgBox "Your Table Style of  " & ans & vbNewLine & "Does not Exist"
End Sub
 
Upvote 0
Try this:

TableStyle will not be in Input box:

Just enter Dark3 or what ever you want.
Pressing Cancel or entering nothing will set Table style to nothing.
Code:
Sub Table_Style()
'Modified 6/29/18 1:35 AM EDT
Dim lb As ListObject
On Error GoTo M
Dim i As Long
Dim ans As String
ans = InputBox("Enter style you want", "To clear table style press Cancel")
For i = 6 To Sheets.Count
    For Each lb In Sheets(i).ListObjects
     If ans <> "" Then
     lb.TableStyle = "TableStyle" & ans
    Else
    lb.TableStyle = ""
    End If
    
    Next
Next
Exit Sub
M:
MsgBox "Your Table Style of  " & ans & vbNewLine & "Does not Exist"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
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