【问题标题】:Error adding code to workbook via VBA通过 VBA 向工作簿添加代码时出错
【发布时间】:2012-11-16 02:31:42
【问题描述】:

我正在尝试在 Excel 中使用 VBA 将条件格式添加到数据透视表的列中。问题是,每当刷新数据透视表或更改过滤器等时,条件格式都会丢失。我的解决方案是向工作簿中的数据透视表更新事件添加一个宏,它可以工作......有点。似乎当我运行创建数据透视表的代码并添加处理条件格式的代码时,会发生错误,但仅在 VBA 窗口未打开时发生。如果 VBA 窗口打开,则代码将正常执行 - 尽管没有代码更改或引用更改。

Private Sub setupConditionalFormattingForStatusColumn()
    Dim thisSheetModule As vbcomponent
    Dim formattingCodeString As String

    On Error GoTo conditionalFormattingError

    formattingCodeString = _
    "Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)" & vbNewLine & _
    "    With Target.parent.Columns(" & harReportColumn("Status") & ")" & vbNewLine & _
    "         .FormatConditions.AddIconSetCondition" & vbNewLine & _
    "         .FormatConditions(.FormatConditions.Count).SetFirstPriority" & vbNewLine & _
    vbNewLine & _
    "         With .FormatConditions(1)" & vbNewLine & _
    "              .IconSet = ActiveWorkbook.IconSets(xl4TrafficLights)" & vbNewLine & _
    "              .IconCriteria(1).Icon = xlIconYellowExclamation" & vbNewLine & _
    vbNewLine & _
    "              With .IconCriteria(2) " & vbNewLine & _
    "                   .Type = xlConditionValueNumber" & vbNewLine & _
    "                   .value = -1" & vbNewLine & _
    "                   .Operator = 5" & vbNewLine & _
    "                   .Icon = xlIconGreenCircle" & vbNewLine & _
    "              End With" & vbNewLine & _
    vbNewLine & _
    "              With .IconCriteria(3)" & vbNewLine & _
    "                   .Type = xlConditionValueNumber" & vbNewLine & _
    "                   .value = 1.05" & vbNewLine & _
    "                   .Operator = 7" & vbNewLine & _
    "                   .Icon = xlIconYellowCircle" & vbNewLine & _
    "              End With" & vbNewLine
    formattingCodeString = formattingCodeString & vbNewLine & _
    "              With .IconCriteria(4)" & vbNewLine & _
    "                   .Type = xlConditionValueNumber" & vbNewLine & _
    "                   .value = 1.15" & vbNewLine & _
    "                   .Operator = 7" & vbNewLine & _
    "                   .Icon = xlIconRedCircleWithBorder" & vbNewLine & _
    "              End With" & vbNewLine & _
    vbNewLine & _
    "             .ShowIconOnly = True" & vbNewLine & _
    "         End With" & vbNewLine & _
    vbNewLine & _
    "         .HorizontalAlignment = xlCenter" & vbNewLine & _
    "         .VerticalAlignment = xlCenter" & vbNewLine & _
    "     End With" & vbNewLine & _
    "End Sub"

    Set thisSheetModule = ThisWorkbook.VBProject.VBComponents(harReportSheet.CodeName)
    thisSheetModule.CodeModule.AddFromString formattingCodeString

    Exit Sub

conditionalFormattingError:
    errorLog.logError "WARNING: An error occured while applying the conditional formatting code for the ""Status"" column."
    Err.Clear
    Resume Next
End Sub

产生错误的行是:thisSheetModule.CodeModule.AddFromString formattingCodeString,但只有在关闭 VBA 窗口时才会产生错误。

有什么想法吗?

【问题讨论】:

  • 你得到什么错误信息?
  • 感谢@KevinPope 没有可用的明确错误消息,因为该错误根本不在/实际上不在 VBA 代码的范围内。但请参阅下面的答案,了解对我有用的方法。

标签: vba excel


【解决方案1】:

所以我能够找到这个问题的答案。显然,当 VBA 窗口未打开时,Excel 没有正确初始化新创建的工作表的代号属性(这里的原因超出了我的范围),但只有在它重新编译时才正确初始化。一种解决方法是在对 codename 属性的任何调用之前强制 Excel 重新编译。对我有用的解决方案是放置以下代码:

On Error Resume Next
Application.VBE.CommandBars.ActiveMenuBar.FindControl(ID:=578).Execute
On Error GoTo conditionalFormattingError

在以 Set thisSheetModule = ... 开头的行上方。奇怪的是,强制重新编译的代码行也给我抛出了一个错误,我可以通过周围的错误处理安全地忽略它。

更多信息可以在这里找到:http://www.office-archive.com/2-excel/d334bf65aeafc392.htm

希望对那里的人有所帮助。 :-)

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多