Conditional Formatting using VBA

llane23

New Member
Joined
Jan 31, 2023
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
I have this code that works fine to highlight, but I don't know how to get it to be a conditional format and to go back to not being highlighted once the condition is met.


Sub HighlightFirstNameLastNameRows()

lastRow = ActiveSheet.Range("A" & Rows.count).End(xlUp).Row
Dim i As Long
For i = 2 To lastRow
If Range("F" & i) = UCase(Range("F" & i)) And Not IsEmpty(Range("F" & i)) Or Range("F" & i) = LCase(Range("F" & i)) And Not IsEmpty(Range("F" & i)) Or Range("G" & i) = UCase(Range("G" & i)) And Not IsEmpty(Range("G" & i)) Or Range("G" & i) = LCase(Range("G" & i)) And Not IsEmpty(Range("G" & i)) Or IsEmpty(Range("F" & i)) Or IsEmpty(Range("G" & i)) Or 1 = Len(Range("F" & i)) Or 1 = Len(Range("G" & i)) Then
Rows(i).Interior.Color = RGB(255, 255, 204)
End If
Next i
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this:
VBA Code:
Sub HighlightFirstNameLastNameRows()

Dim i As Long, lastRow As Long

lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow
    If Range("F" & i) = UCase(Range("F" & i)) And Not IsEmpty(Range("F" & i)) Or Range("F" & i) = LCase(Range("F" & i)) And Not IsEmpty(Range("F" & i)) Or Range("G" & i) = UCase(Range("G" & i)) And Not IsEmpty(Range("G" & i)) Or Range("G" & i) = LCase(Range("G" & i)) And Not IsEmpty(Range("G" & i)) Or IsEmpty(Range("F" & i)) Or IsEmpty(Range("G" & i)) Or 1 = Len(Range("F" & i)) Or 1 = Len(Range("G" & i)) Then
        Rows(i).Interior.Color = RGB(255, 255, 204)
    Else
        Rows(i).Interior.Pattern = xlNone
    End If
Next i

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,087
Messages
6,123,046
Members
449,092
Latest member
ikke

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