+ Reply to Thread
Results 1 to 7 of 7

Macro failing at conditional formatting font colour

Hybrid View

  1. #1
    Registered User
    Join Date
    11-04-2013
    Location
    UK
    MS-Off Ver
    2016 (Office 365 Business Premium)
    Posts
    7

    Post Macro failing at conditional formatting font colour

    Hi there,

    I've recorded a macro in Excel 2007 which opens a file then highlights columns I to CH, then applies conditional formatting so that if a cell has a value of <0, it applies a red font with pink fill colour. Here's the code:

    Formula: copy to clipboard
    Sub highlight()
    '
    ' highlight Macro
    '

    '
    ChDir "S:\Reports"
    Workbooks.Open Filename:="S:\Reports\78weeks.xls"
    Columns("I:I").Select
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 12
    ActiveWindow.ScrollColumn = 15
    ActiveWindow.ScrollColumn = 27
    ActiveWindow.ScrollColumn = 29
    ActiveWindow.ScrollColumn = 37
    ActiveWindow.ScrollColumn = 40
    ActiveWindow.ScrollColumn = 50
    ActiveWindow.ScrollColumn = 54
    ActiveWindow.ScrollColumn = 59
    ActiveWindow.ScrollColumn = 60
    ActiveWindow.ScrollColumn = 62
    Columns("I:CH").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
    Formula1:="=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
    .Color = -16383844
    .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 13551615
    .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("A1:A2").Select
    ActiveWorkbook.Save
    ActiveWindow.Close
    End Sub


    The problem is, when I run it, it fails and hitting debug takes me to the below lines, highlighted.

    Formula: copy to clipboard
    With Selection.FormatConditions(1).Font
    .Color = -16383844


    If anyone could point me in the right direction it would be greatly appreciated.
    Last edited by RC-AMG; 01-12-2015 at 12:28 PM. Reason: Solved.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Macro failing at conditional formatting font colour

    Try this:
    Sub highlight()
    
    '
    
    ' highlight Macro
    
        Workbooks.Open Filename:="S:\Reports\78weeks.xls"
    
        With Columns("I:CH")
    
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=0"
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
    
            With .FormatConditions(1)
                With .Font
                    .Color = 393372
                    .TintAndShade = 0
                End With
    
                With .Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 13551615
                    .TintAndShade = 0
                End With
    
                .StopIfTrue = False
    
            End With
    
        End With
    
        Range("A1:A2").Select
    
        ActiveWorkbook.Save
    
        ActiveWindow.Close
    
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    11-04-2013
    Location
    UK
    MS-Off Ver
    2016 (Office 365 Business Premium)
    Posts
    7

    Re: Macro failing at conditional formatting font colour

    Thanks for the quick reply! Unfortunately this gives me the same kind of error, Run-time '1004'/Application-defined or object-defined error, highlighting the row beginning '.colour' at this point:
    Formula: copy to clipboard
    With .FormatConditions(1)
    With .Font
    .Color = 393372
    .TintAndShade = 0
    End With

    I ran a repair install of Office 2007, just in case, but still no joy.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Macro failing at conditional formatting font colour

    A couple of questions:
    1. Is the worksheet protected in any way?
    2. Do you have at least SP2 installed on your Office 2007?

  5. #5
    Registered User
    Join Date
    11-04-2013
    Location
    UK
    MS-Off Ver
    2016 (Office 365 Business Premium)
    Posts
    7

    Re: Macro failing at conditional formatting font colour

    1. No protection
    2. Service Pack 3

    Cheers.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Macro failing at conditional formatting font colour

    I can't see why it wouldn't work then - it works fine in 2010 (using your original code also). Can you post a sample workbook that it fails on? (no data necessary)

    Noting that your workbook is in xls format, I assume you are aware that since an xls file colour palette can only use 56 colours, you may not get the exact colour you specify.

  7. #7
    Registered User
    Join Date
    11-04-2013
    Location
    UK
    MS-Off Ver
    2016 (Office 365 Business Premium)
    Posts
    7

    Re: Macro failing at conditional formatting font colour

    I've uninstalled 2007 and moved to 2013, and it runs fine; like you mentioned with 2010. Very odd. Oh well, thanks! And thanks for the info re XLS colour palette.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Conditional colour formatting without seeing the font used for the condition.
    By kristyhutt in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-18-2013, 01:07 AM
  2. Replies: 1
    Last Post: 09-12-2013, 02:09 PM
  3. Replies: 15
    Last Post: 05-31-2012, 10:34 AM
  4. Replies: 2
    Last Post: 10-03-2009, 05:01 PM
  5. Replies: 9
    Last Post: 10-22-2007, 07:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1