Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:37:29+00:00 2026-06-14T07:37:29+00:00

I am trying to use VBA in Excel to add conditional formatting to a

  • 0

I am trying to use VBA in Excel to add conditional formatting to a column of a pivot table. The issue is that whenever the pivot table is refreshed, or a filter is changed, etc. the conditional formatting is lost. My solution was to add a macro to the pivot table update event in the workbook, which works … kinda. It seems that when I run the code that creates the pivot table and adds the code to handle conditional formatting an error occurs but ONLY when the VBA window is NOT open. If the VBA window is open the code executes normally – despite no code changes or reference changes.

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

The line which generates the error is: thisSheetModule.CodeModule.AddFromString formattingCodeString but the error is only generated if the VBA window is closed.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:37:31+00:00Added an answer on June 14, 2026 at 7:37 am

    So I was able to find an answer to this issue. Evidently Excel does not properly initialize the codename property of newly created worksheets when the VBA window is not open (the why here is beyond me) but only when it recompiles. A work-around is to force Excel to recompile prior to any calls to the codename property. The solution which worked for me was to place the following code:

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

    above the line beginning with Set thisSheetModule = ... . Oddly enough the line of code which forces the recompile also throws an error for me which I was able to safely ignore with the surrounding error handling.

    More information can be found here: http://www.office-archive.com/2-excel/d334bf65aeafc392.htm

    Hope that helps someone out there. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Excel VBA to write to a text file. I'm doing
VBA newbie over here. I'm trying to use an array formula through excel vba
I am trying to use the Worksheet Change Event in Excel VBA, but it
I'm using VBA in Excel and am trying to use the popular ShellAndWait subroutine.
I'm trying to write an Excel add-in that can get data from a web
Trying to use Excel VBA to capture all the file attributes from files on
I have an excel vba problem that I am trying to solve and after
I am trying to write some vba code in Excel to query a table
I'm trying to use the DateDiff function for a macro in excel using VBA.
I am trying to use batch updates in Access VBA. My code works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.