【发布时间】:2009-11-09 06:18:08
【问题描述】:
我正在使用以下函数来创建表格样式:
Public Function CreateTableStyle()
ActiveWorkbook.Unprotect
Dim objTS As TableStyle
On Error Resume Next
Set objTS = ActiveWorkbook.TableStyles("MyTableStyle")
On Error GoTo err_CreateTableStyle
If Not objTS Is Nothing Then
Exit Function
End If
Set objTS = ActiveWorkbook.TableStyles.Add("MyTableStyle")
With ActiveWorkbook.TableStyles("MyTableStyle")
.ShowAsAvailablePivotTableStyle = True
.ShowAsAvailableTableStyle = False
End With
With ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlHeaderRow).Font
.FontStyle = "Bold"
.TintAndShade = 0
.ThemeColor = xlThemeColorDark1
End With
With ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlHeaderRow).Interior
.ThemeColor = xlThemeColorLight2
.TintAndShade = -0.249946592608417
End With
With ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlTotalRow).Font
.FontStyle = "Bold"
.TintAndShade = 0
.ThemeColor = xlThemeColorDark1
End With
With ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlTotalRow).Interior
.ThemeColor = xlThemeColorLight2
.TintAndShade = -0.249946592608417
End With
ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlSubtotalRow1).Font.FontStyle = "Bold"
With ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlSubtotalRow1).Interior
.Color = 16764828
.TintAndShade = 0
End With
ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlSubtotalRow2).Font.FontStyle = "Bold"
With ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlSubtotalRow2).Interior
.Color = 16777164
.TintAndShade = 0
End With
ActiveWorkbook.Protect
Exit Function
err_CreateTableStyle:
Call Common.ErrRaise(Erl, "Common", "CreateTableStyle", "CreateTableStyle")
End Function
在下一行:
With ActiveWorkbook.TableStyles("MyTableStyle").TableStyleElements( _
xlHeaderRow).Font
.FontStyle = "Bold"
我收到一个错误:
运行时错误“1004” 无法设置 Font 类的 FontStyle 属性。
有人可以确定问题吗?我无法弄清楚为什么它不让我设置属性。
【问题讨论】:
-
代码正在运行。我不确定为什么它不应该与该特定行一起使用。您能否删除错误处理程序(在错误恢复下一个和错误转到...时)并查看它在哪里失败?
-
在同一个语句中失败。我正在尝试通过 XLA 运行此代码。它通过 XLA 工作吗?它通过宏工作,但在我的情况下不是 XLA。
-
改为设置 Bold 属性怎么样?这行得通吗?
-
抛开那句话,行为是什么?即注释失败的行并查看当它踩到设置
TintAndShade属性的代码时会发生什么。