2012-11-13 40 views
0

我想在Excel中使用VBA將條件格式添加到數據透視表的列中。問題是無論何時刷新數據透視表或更改過濾器等,條件格式都會丟失。我的解決方案是在工作簿中的數據透視表更新事件中添加一個宏,該工作簿很有效。看來,當我運行創建數據透視表的代碼並添加處理條件格式的代碼時,會發生錯誤,但只有在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窗口僅產生的錯誤。

任何想法?

+7

你得到了什麼錯誤信息? –

+0

謝謝@KevinPope沒有明確的錯誤消息可用,因爲錯誤不是/完全不在VBA代碼的範圍內。但請參閱下面的答案,瞭解我的工作。 – Billy

回答

0

所以我找到了這個問題的答案。很顯然,當VBA窗口未打開時(這是爲什麼超出我的原因),只有當它重新編譯時,Excel才能正確初始化新創建的工作表的代碼名屬性。解決方法是在對代碼名屬性進行任何調用之前強制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

希望幫助別人那裏。 :-)

相關問題