If no table totals

blaisjoel

New Member
Joined
Oct 28, 2016
Messages
26
I am trying to run a macro to edit a table's formatting including table totals. Some tables do not have table totals and this is creating an error. How do I insert an is error inside my code?

Code:
   Range("A2").Select
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium8"
    Range("Table1[#Headers]").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 192
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    Range("Table1[#Totals]").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 192
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    Selection.Font.Bold = True
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,835
Office Version
  1. 2010
Platform
  1. Windows
Try this

Code:
    Range("A2").Select
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium8"
    Range("Table1[#Headers]").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 192
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
[B][COLOR=#ff0000]    On Error Resume Next[/COLOR][/B]
        Range("Table1[#Totals]").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 192
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With Selection.Font
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
        End With
        Selection.Font.Bold = True
[B][COLOR=#ff0000]    On Error GoTo 0[/COLOR][/B]
 
Solution

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,835
Office Version
  1. 2010
Platform
  1. Windows
Or in this way too:


Code:
    Range("A2").Select
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium8"
    Range("Table1[#Headers]").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 192
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    
    If ActiveSheet.ListObjects("Table1").ShowTotals Then
        Range("Table1[#Totals]").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 192
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With Selection.Font
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
        End With
        Selection.Font.Bold = True
    End If
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,835
Office Version
  1. 2010
Platform
  1. Windows
Another way is by inserting the totals in the table

Code:
    Range("A2").Select
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium8"
    Range("Table1[#Headers]").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 192
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    
[COLOR=#0000ff]    If ActiveSheet.ListObjects("Table1").ShowTotals = False Then[/COLOR]
[COLOR=#0000ff]        ActiveSheet.ListObjects("Table1").ShowTotals = True[/COLOR]
[COLOR=#0000ff]    End If[/COLOR]
    
    Range("Table1[#Totals]").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 192
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    Selection.Font.Bold = True
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,835
Office Version
  1. 2010
Platform
  1. Windows
I'm glad to help you. I appreciate your kind comments.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,651
Messages
5,838,582
Members
430,557
Latest member
MK15

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
Top