VBA, Highlight If Text AND Dated After Specific Year

NEW_2VBA

Board Regular
Joined
Dec 15, 2015
Messages
106
Hello!
I used the following code to highlight an entire row if column F contained specific text, but I'm now struggling with how to also specify that it must be dated after 1/1/2013. So I'm looking to include those two conditions without using conditional formatting. :unsure:

Any feedback is greatly appreciated. Thank YOU!!! ?

Dim Cell As Range, Addr As String
With Range("F1", Cells(Rows.Count, "F").End(xlUp))
Set Cell = .Find("*Unit Costs*", , xlValues, , , , False, , False)
If Not Cell Is Nothing Then
Addr = Cell.Address
Do
Cell.EntireRow.Interior.Color = RGB(230, 184, 183)
Set Cell = .FindNext(Cell)
Loop While Not Cell Is Nothing And Cell.Address <> Addr
End If
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try:
VBA Code:
Sub test()
Dim Cell As Range, Addr As String
With Range("F1", Cells(Rows.Count, "F").End(xlUp))
    Set Cell = .Find("*Unit Costs*", , xlValues, , , , False, , False)
    If Not Cell Is Nothing Then
        Addr = Cell.Address
    End If
    Do
    If Cell.Offset(0, 3).Value > #1/1/2013# Then
        Cell.EntireRow.Interior.Color = RGB(230, 184, 183)
    End If
        Set Cell = .FindNext(Cell)
        If Cell Is Nothing Then Exit Do
        If Cell.Address = Addr Then Exit Do
    Loop
End With
End Sub
 
Upvote 0
Sorry, another text entry was requested to be included.
I amended the .Find portion of the code as follows to include both but that doesn't seem to work. I appreciate any feedback, thanks!

Set Cell = .Find("*MILESTONE*,*Unit Costs*", , xlValues, , , , False, , False)
 
Upvote 0
Do you expect there are cells that contain both sub-strings or what? You need to tell us what exactly you want to do.
 
Upvote 0
Cells will either contain one or the other sub-strings, so I would need to highlight rows with text "Unit Costs" or "Milestone" dated 1/1/2012+.
 
Upvote 0
This will highlight rows with col F cells containing "unit costs" and a date greater than 1/1/2013 in col I, or col F cells containing "milestone" and a date greater than 1/1/2012 in col I.
VBA Code:
Sub test()
Dim Cell As Range, Addr As String, Strs(1 To 2), Dts(1 To 2)
Strs(1) = "*Unit Costs*": Strs(2) = "*MILESTONE*"
Dts(1) = #1/1/2013#: Dts(2) = #1/1/2012#
For i = 1 To 2
    With Range("F1", Cells(Rows.Count, "F").End(xlUp))
        Set Cell = .Find(Strs(i), , xlValues, , , , False, , False)
        If Not Cell Is Nothing Then
            Addr = Cell.Address
        End If
        Do
        If Cell.Offset(0, 3).Value > Dts(i) Then
            Cell.EntireRow.Interior.Color = RGB(230, 184, 183)
        End If
            Set Cell = .FindNext(Cell)
            If Cell Is Nothing Then Exit Do
            If Cell.Address = Addr Then Exit Do
        Loop
    End With
Next i
End Sub
 
Upvote 0
I can't say thank you enough for your help! It worked! ? ?

I noticed however that the spreadsheet must contain both text strings. That how I got it to work.
The spreadsheet will either have: 1) Both text strings
2) One or the other
3) Neither
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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