【问题标题】:Error while creating a table style in excel在 Excel 中创建表格样式时出错
【发布时间】: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 属性的代码时会发生什么。

标签: xla excel vba


【解决方案1】:

XLA 的行为有时可能会有所不同......它们在 Excel 启动时加载/执行,在任何工作表(甚至是空的工作表)打开之前,因此假设打开的书的 XLA 将失败

如果此代码旨在设置一些“默认格式”...

...我宁愿设置格式并将整个工作表保存为工作表或工作簿模板 (XLT),然后使用该模板创建新文件,

... 或 - 再次在模板中,存储上述代码并从像 Workbook_NewSheet() 或 Workbook_Open() 这样的事件宏中调用它

【讨论】:

    【解决方案2】:

    试试 .font.bold = true

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多