+ Reply to Thread
Results 1 to 10 of 10

VBA to apply conditional format

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    VBA to apply conditional format

    Hi all,

    I am applying several conditional formats to a sheet with macros. All of them are set up successfully but the one below:

    With ThisWorkbook.Sheets("Test").Range("$D$13:$D$1500,$S$13:$T$1500,$V$13:$AB$1500,$AT$13:$BE$1500,$CI$13:$CK$1500,$CO$13:$CQ$1500,$DA$13:$EC$1500,$FN$13:$FQ$1500,$FU$13:$FW$1500,$GG$13:$HI$1500,$IT$13:$IW$1500,$JA$13:$JC$1500,$JM$13:$KO$1500,$LZ$13:$MC$1500,$MG$13:$MI$1500,$MS$13:$NU$1500,$PF$13:$PI$1500,$PM$13:$PO$1500,$PY$13:$RA$1500,$SL$13:$SO$1500,$SS$13:$SU$1500,$TE$13:$UG$1500,$VR$13:$VU$1500,$VY$13:$WA$1500,$O$13:$P$1500")
            .FormatConditions.Add Type:=xlExpression, Formula1:="=OR($D13=""DROP"",$E13=""INACTIVE"")"
            With .FormatConditions(.FormatConditions.Count)
                 .SetFirstPriority
                 .StopIfTrue = True
                With .Interior
                     .Color = RGB(178, 188, 198)
                End With
                    With .Font
                         .Bold = False
                         .Italic = False
                         .Color = RGB(132, 146, 160)
                    End With
            End With
        End With
    I get a run-time error 1004. I assume there's too many ranges referenced? I tried Union(Range( as well, but no luck...

  2. #2
    Forum Expert kersplash's Avatar
    Join Date
    11-22-2016
    Location
    Perth
    MS-Off Ver
    Home 2016 (Windows 10)/Work 2013 Pro Plus (Windows 10)
    Posts
    2,012

    Re: VBA to apply conditional format

    Try reducing the number of ranges and see if you get the error.

  3. #3
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to apply conditional format

    I tried before and it worked! Hence, I assume the number of ranges causes the error...

  4. #4
    Forum Expert kersplash's Avatar
    Join Date
    11-22-2016
    Location
    Perth
    MS-Off Ver
    Home 2016 (Windows 10)/Work 2013 Pro Plus (Windows 10)
    Posts
    2,012

    Re: VBA to apply conditional format

    I found on another forum that the number of ranges maximum is 16. (EDIT - may not be accurate on second look).

    And recommend using UNION to join the ranges.

  5. #5
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to apply conditional format

    Quote Originally Posted by kersplash View Post
    I found on another forum that the number of ranges maximum is 16. (EDIT - may not be accurate on second look).

    And recommend using UNION to join the ranges.
    Would using Union increase such a potential maximum?

  6. #6
    Forum Expert kersplash's Avatar
    Join Date
    11-22-2016
    Location
    Perth
    MS-Off Ver
    Home 2016 (Windows 10)/Work 2013 Pro Plus (Windows 10)
    Posts
    2,012

    Re: VBA to apply conditional format

    Is it possible there is a syntax error in one of the ranges?

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: VBA to apply conditional format

    The address that you pass to range can't be longer than 255 characters - yours is 392. Even if you remove the unnecessary $ signs you are left with 292, so you do need to union 2 ranges, or apply the CF in two blocks.

  8. #8
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to apply conditional format

    How would such a Union Range look like? I tried:

    With ThisWorkbook.Sheets("Test").Union(Range("$D$13:$D$1500,$S$13:$T$1500,$V$13:$AB$1500,$AT$13:$BE$1500,$CI$13:$CK$1500,$CO$13:$CQ$1500,$DA$13:$EC$1500,$FN$13:$FQ$1500,$FU$13:$FW$1500,$GG$13:$HI$1500,$IT$13:$IW$1500,$JA$13:$JC$1500,$JM$13:$KO$1500"), Range("$LZ$13:$MC$1500,$MG$13:$MI$1500,$MS$13:$NU$1500,$PF$13:$PI$1500,$PM$13:$PO$1500,$PY$13:$RA$1500,$SL$13:$SO$1500,$SS$13:$SU$1500,$TE$13:$UG$1500,$VR$13:$VU$1500,$VY$13:$WA$1500,$O$13:$P$1500"))
            .FormatConditions.Add Type:=xlExpression, Formula1:="=OR($D13=""DROP"",$E13=""INACTIVE"")"
            With .FormatConditions(.FormatConditions.Count)
                 .SetFirstPriority
                 .StopIfTrue = True
                With .Interior
                     .Color = RGB(178, 188, 198)
                End With
                    With .Font
                         .Bold = False
                         .Italic = False
                         .Color = RGB(132, 146, 160)
                    End With
            End With
        End With
    But it gets me a "Object doesn't support this property or method" error...

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: VBA to apply conditional format

    Union is not a worksheet property. You should use:

    With Union(ThisWorkbook.Sheets("Test").Range("D13:D1500,S13:T1500,V13:AB1500,AT13:BE1500,CI13:CK1500,CO13:CQ1500,DA13:EC1500,FN13:FQ1500,FU13:FW1500,GG13:HI1500,IT13:IW1500,JA13:JC1500,JM13:KO1500"), ThisWorkbook.Sheets("Test").Range("LZ13:MC1500,MG13:MI1500,MS13:NU1500,PF13:PI1500,PM13:PO1500,PY13:RA1500,SL13:SO1500,SS13:SU1500,TE13:UG1500,VR$13:VU1500,VY13:WA1500,O13:P1500"))

  10. #10
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to apply conditional format

    Thank you!! That works

+ 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] apply conditional format based 2 criterias
    By bdouglas1011 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-28-2017, 06:43 AM
  2. [SOLVED] How To Apply Conditional Format To Cell
    By Kingswood in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-09-2015, 12:59 PM
  3. apply conditional format to every 4th row
    By jlanderson18 in forum Excel General
    Replies: 2
    Last Post: 07-18-2013, 01:39 PM
  4. Only apply conditional format if cell val > 0
    By moorzee in forum Excel General
    Replies: 2
    Last Post: 05-10-2013, 05:02 PM
  5. Apply Conditional format only when cells are active?
    By bob63el in forum Excel General
    Replies: 3
    Last Post: 04-03-2013, 10:18 PM
  6. Replies: 9
    Last Post: 10-29-2012, 06:06 PM
  7. Conditional Format-apply to every row
    By hassankhan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2009, 03:24 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