variant with conditional formatting

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I'm having a difficult time with how I can use a variant with a conditional formatting formula.

This is what I have

VBA Code:
    Dim rng As Variant
    Set rng = Range("V2:V" & Range("A" & Rows.Count).End(xlUp).Row)
    
    rng.FormatConditions.Add Type:=xlTextString, String:="No", TextOperator:=xlContains
    rng.FormatConditions(1).Interior.Color = 255 ' Red
    rng.FormatConditions(1).Font.ThemeColor = xlThemeColorDark1
    rng.FormatConditions(1).Font.Bold = True
    rng.FormatConditions(1).StopIfTrue = False
    
    rng.FormatConditions.Add Type:=xlTextString, String:="Yes", TextOperator:=xlContains
    rng.FormatConditions(2).Interior.Color = 5287936 ' Green
    rng.FormatConditions(2).Font.ThemeColor = xlThemeColorDark1
    rng.FormatConditions(2).Font.Bold = True
    rng.FormatConditions(2).StopIfTrue = False
    
    rng.Offset(0, 1).FormatConditions.Add Type:=xlExpression, Formula1:="=$V$35=""No""" 'this is where I'm hung up
    rng.FormatConditions(3).Interior.Color = 255 ' Red
    rng.FormatConditions(3).Font.ThemeColor = xlThemeColorDark1
    rng.FormatConditions(3).Font.Bold = True
    rng.FormatConditions(3).StopIfTrue = False

condition3 is where I'm having trouble. What I'm trying to accomplish is if any cell in column V = "No", then apply the red background to both cells in V and W. Perhaps there is a better way to write this?

Thank you
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How about
VBA Code:
Rng.Resize(, 2).FormatConditions.Add Type:=xlExpression, Formula1:="=$V2=""No""" 'this is where I'm hung up
 
Upvote 0
How about
VBA Code:
Rng.Resize(, 2).FormatConditions.Add Type:=xlExpression, Formula1:="=$V2=""No""" 'this is where I'm hung up
Got it, so the formula only needs to point at the first cell. Thanks.

I made the change and the condition was created, but stopped when it tried to apply the formatting.

This worked to create the condition
VBA Code:
rng.Offset(0, 1).FormatConditions.Add Type:=xlExpression, Formula1:="=$V$2=""No"""

Run-time error '9': Subscript out of range
VBA Code:
rng.FormatConditions(3).Interior.Color = 255

I got the same error when I added the offset
VBA Code:
rng.Offset(0, 1).FormatConditions(3).Interior.Color = 255
 
Upvote 0
Do you want both cols V & W to get the red fill?
 
Upvote 0
Should that also apply to the Yes?
 
Upvote 0
Ok, how about
VBA Code:
    Dim rng As Range
    Set rng = Range("V2:V" & Range("A" & Rows.Count).End(xlUp).Row)
    
    With rng
      .FormatConditions.Add Type:=xlTextString, String:="Yes", TextOperator:=xlContains
      With .FormatConditions(.FormatConditions.Count)
         .Interior.Color = 5287936 ' Green
         .Font.ThemeColor = xlThemeColorDark1
         .Font.Bold = True
         .StopIfTrue = False
      End With
   End With
    
   With rng.Resize(, 2)
      .FormatConditions.Add Type:=xlExpression, Formula1:="=$V2=""No"""
      With .FormatConditions(.FormatConditions.Count)
         .Interior.Color = 255 ' Red
         .Font.ThemeColor = xlThemeColorDark1
         .Font.Bold = True
         .StopIfTrue = False
      End With
   End With
 
Upvote 0
Solution
Ok, how about
VBA Code:
    Dim rng As Range
    Set rng = Range("V2:V" & Range("A" & Rows.Count).End(xlUp).Row)
   
    With rng
      .FormatConditions.Add Type:=xlTextString, String:="Yes", TextOperator:=xlContains
      With .FormatConditions(.FormatConditions.Count)
         .Interior.Color = 5287936 ' Green
         .Font.ThemeColor = xlThemeColorDark1
         .Font.Bold = True
         .StopIfTrue = False
      End With
   End With
   
   With rng.Resize(, 2)
      .FormatConditions.Add Type:=xlExpression, Formula1:="=$V2=""No"""
      With .FormatConditions(.FormatConditions.Count)
         .Interior.Color = 255 ' Red
         .Font.ThemeColor = xlThemeColorDark1
         .Font.Bold = True
         .StopIfTrue = False
      End With
   End With
Perfect, thank you. I haven't used Resize before. Nice tip, thanks again
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top