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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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