phil652
phil652

Reputation: 1506

How to find cells that contain specific text then hide the entire row

When a button is pressed I want to loop through all the cells in my Sheet and find the cells that contains .doc or .xls or .pdf and hide the entire Row. I know I can't use Contains but there must be something similar.

Cell example PM-TR Training.doc

This is what I have for now what can I replace contains with?

Sub HideRows()
    Dim cell As Range
    Dim DataCount As Integer
    'Change the sheet name as necessary in the following line
    With Worksheets("Sheet1")
        DataCount = Range("A" & Rows.Count).End(xlUp).Row
        For Each cell In Range("A1:A" & DataCount)
            If cell.Contains(".doc") Or cell.Contains(".xls") Or cell.Contains(".pdf") Then
                'The following code assumes you want the row hidden.
                Range(cell.Row).EntireRow.Hidden = True
            End If
        Next cell
    End With
End Sub

Upvotes: 1

Views: 6296

Answers (1)

basodre
basodre

Reputation: 5770

The function InStr withing your IF statement should do the trick.

IF instr(cell.value, ".doc")> 0 then
    'Code Here

Upvotes: 2

Related Questions