Expand Existing Macro to Not Select Empty/Blank Cells

norts55

Board Regular
Joined
Jul 27, 2012
Messages
184
Hello,
I have this macro that selects cells based on a font color. I need to expand this so it also only selects cells that have a value (text in the cell) and/or doesn't select empty cells (No text in the cell). The reason for this is the cells are sometimes copied around the sheet, then their contents are cleared but the formatting is still there and when this macro is ran it finds the empty cells.

I have tried this with if statements but can't seem to figure it out.

Any help would be greatly appreciated. Thanks in advance.


Code:
Sub z_Matls()
    
    Dim rngFound As Range
    Dim rngAll As Range
    Dim strFirst As String
    Dim lColor As Long
    
    lColor = RGB(153, 51, 0)
    
    With Application.FindFormat
        .Clear
        .Font.Color = lColor        'Search for font color
        '.Interior.Color = lColor   'Search for background color
    End With
    
    With ActiveSheet.UsedRange
        Set rngFound = .Find("", .Cells(.Cells.Count), SearchFormat:=True)
        If Not rngFound Is Nothing Then
            Set rngAll = rngFound
            strFirst = rngFound.Address
            Do
                Set rngAll = Union(rngAll, rngFound)
                Set rngFound = .Find("", rngFound, SearchFormat:=True)
            Loop While rngFound.Address <> strFirst
            
            rngAll.Select
            
            Set rngAll = Nothing
            Set rngFound = Nothing
            strFirst = vbNullString
        Else
            MsgBox "No cells found with font color " & lColor & ".", , "No Matches"
            'MsgBox "No cells found with background color " & lColor & ".", , "No Matches"
        End If
    End With
    
    Application.FindFormat.Clear
    
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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