+ Reply to Thread
Results 1 to 2 of 2

Runtime error 9 with conditional format macro

Hybrid View

  1. #1
    Registered User
    Join Date
    12-22-2011
    Location
    Berlin, Germany
    MS-Off Ver
    Excel 2010
    Posts
    58

    Runtime error 9 with conditional format macro

    Hi. I have a macro that first deletes the present conditional formats on the tab and then creates a new one. When I run the code, I get "Run-time error 9 - Subscript out of range and it highlights the line "Columns("H:H").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority". Why is this happening and how do I fix it? I've tried inserting selection instead of columns("H:H") with no luck. The strange thing is that sometimes (1/5 times) this code runs fine. Any insight would be appreciated. Thanks!

    Sub Macro5()
    Sheets("Mag-Alum").Select
        With Range("1:1048576")
            .FormatConditions.Delete
        End With
    With Mag - Alum
        Columns("H:H").Select
        Columns("H:H").FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=AND(CELL(""type"",$G1)=""v"",CELL(""type"",$H1)=""v"",$H1-$G1<'Conv. Chart'!$J$2)"
        Columns("H:H").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Columns("H:H").FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
        End With
        With Columns("H:H").FormatConditions(1).StopIfTrue = False
    End With
    End With
        Range("A1").Select
    End Sub

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Runtime error 9 with conditional format macro

    If you use the With command, the statements between the With and End with like Range, Columns, Rows, etc should have the . (dot) in front of them.

    Try this
    Sub Macro5()
    
    With Worksheets("Mag - Alum")
        .Cells .FormatConditions.Delete
        .Columns("H:H").FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=AND(CELL(""type"",$G1)=""v"",CELL(""type"",$H1)=""v"",$H1-$G1<'Conv. Chart'!$J$2)"
        .Columns("H:H").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With .Columns("H:H").FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
        End With
        .Columns("H:H").FormatConditions(1).StopIfTrue = False
        .Range("A1").Select
    End With
        
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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