将代码通过VBA添加到工作簿时出错

时间:2012-11-13 18:42:04

标签: excel vba excel-vba

我正在尝试在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窗口关闭时才会生成错误。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

所以我能够找到这个问题的答案。显然Excel没有正确初始化新创建的工作表的代号名称属性,当VBA窗口未打开时(为什么这里超出我),但只有在重新编译时才这样做。解决方法是强制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

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