Q: 使用 VBA 创建一个新的 Pivot TableStyle

Q: Use VBA to create a new Pivot TableStyle

我录制了一个宏来创建一个新的数据透视表 Table 样式,这样每当我创建一个新的工作表时,这个数据透视表 Table 样式就会被添加为工作簿的默认样式。但是,当我尝试在新工作簿上 运行 它时,它似乎不起作用。起初,我认为它可能是名称(即 Sheet1),但即使所有内容都匹配,它也会在第一行出错。我讨厌不得不在我制作的每份报告中添加这种新的枢轴 table 样式,所以如果有人有任何提示,我将不胜感激。我是 VBA 的完全新手,所以如果有任何缩短此代码的建议,那也会有很大帮助!

编辑:代码没有大引号——那是我为了发布而重命名的。

附加编辑: 这是我得到的错误: VBA Error: Run-time error '5': invalid procedure call or arguement

当我点击“调试”时,它会将我带到以黄色突出显示的第一行代码:ActiveWorkbook.TableStyles.Add(“概述”)

Sub Overview_Pivot_Format()
'
' Overview_Pivot_Format Macro
'

'
    ActiveWorkbook.TableStyles.Add (“Overview”)
    With ActiveWorkbook.TableStyles(“Overview”)
        .ShowAsAvailablePivotTableStyle = True
        .ShowAsAvailableTableStyle = False
        .ShowAsAvailableSlicerStyle = False
        .ShowAsAvailableTimelineStyle = False
    End With
    ActiveWorkbook.DefaultPivotTableStyle = “Overview”
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlWholeTable).Borders(xlEdgeTop)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlWholeTable).Borders(xlEdgeBottom)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlWholeTable).Borders(xlEdgeLeft)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlWholeTable).Borders(xlEdgeRight)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlWholeTable).Borders(xlInsideVertical)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlWholeTable).Borders(xlInsideHorizontal)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlHeaderRow).Interior
        .Color = 15658734
        .TintAndShade = 0
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlHeaderRow).Borders(xlEdgeTop)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlHeaderRow).Borders(xlEdgeBottom)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlHeaderRow).Borders(xlEdgeLeft)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlHeaderRow).Borders(xlEdgeRight)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlHeaderRow).Borders(xlInsideVertical)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlHeaderRow).Borders(xlInsideHorizontal)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlTotalRow).Font
        .FontStyle = "Bold"
        .TintAndShade = 0
        .ThemeColor = xlThemeColorDark1
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlTotalRow).Interior
        .Color = 6697728
        .TintAndShade = 0
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlSubtotalRow1).Font
        .FontStyle = "Bold"
        .TintAndShade = 0
        .ThemeColor = xlThemeColorDark1
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlSubtotalRow1).Interior
        .Color = 6697728
        .TintAndShade = 0
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlSubtotalRow2).Font
        .FontStyle = "Bold"
        .TintAndShade = 0
        .ThemeColor = xlThemeColorDark1
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlSubtotalRow2).Interior
        .Color = 6697728
        .TintAndShade = 0
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlSubtotalRow3).Font
        .FontStyle = "Bold"
        .TintAndShade = 0
        .ThemeColor = xlThemeColorDark1
    End With
    With ActiveWorkbook.TableStyles(“Overview”).TableStyleElements( _
        xlSubtotalRow3).Interior
        .Color = 6697728
        .TintAndShade = 0
    End With
End Sub

以下对我有用:

Sub Overview_Pivot_Format()
    
    Dim ts As TableStyle, wb As Workbook
    
    Set wb = ActiveWorkbook 'workbook to be updated
    
    On Error Resume Next              'ignore error if no style found in next line
    wb.TableStyles("OverView").Delete 'in case already present
    On Error GoTo 0                   'stop ignoring errors
    Set ts = wb.TableStyles.Add("Overview") 'get a reference to the added style
    
    With ts
        .ShowAsAvailablePivotTableStyle = True
        .ShowAsAvailableTableStyle = False
        .ShowAsAvailableSlicerStyle = False
        .ShowAsAvailableTimelineStyle = False
    End With
    wb.DefaultPivotTableStyle = ts
    'set properties by calling the 3 subs below...
    DoBorders ts.TableStyleElements(xlWholeTable)
    
    DoInterior ts.TableStyleElements(xlHeaderRow), 15658734
    DoBorders ts.TableStyleElements(xlHeaderRow)
    
    DoInterior ts.TableStyleElements(xlTotalRow), 6697728
    DoFont ts.TableStyleElements(xlTotalRow)
    
    DoInterior ts.TableStyleElements(xlSubtotalRow1), 6697728
    DoFont ts.TableStyleElements(xlSubtotalRow1)
    
    DoInterior ts.TableStyleElements(xlSubtotalRow2), 6697728
    DoFont ts.TableStyleElements(xlSubtotalRow2)
    
    DoInterior ts.TableStyleElements(xlSubtotalRow3), 6697728
    DoFont ts.TableStyleElements(xlSubtotalRow3)
     
End Sub

'next 3 sub take care of updating the styles...
Sub DoFont(tse As TableStyleElement)
    With tse.Font
        .FontStyle = "Bold"
        .TintAndShade = 0
        .ThemeColor = xlThemeColorDark1
    End With
End Sub

Sub DoBorders(tse As TableStyleElement)
    With tse.Borders() 'no need to set individually...
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
End Sub

Sub DoInterior(tse As TableStyleElement, clr As Long)
    With tse.Interior
        .Color = clr
        .TintAndShade = 0
    End With
End Sub