Search a column for a value, hide rows that do not contain that value

Blanchetdb

Board Regular
Joined
Jul 31, 2018
Messages
161
Office Version
  1. 2016
Platform
  1. Windows
Hi

So I am looking to have a code that will search thru column P (range P2:P2000); search for the name of a person ex. Jane Doe (value in cell C6). If found, the corresponding row would be visible but rows that do have than name within that column, the row would be hidden

So I only want to be able to see the rows that contain the name entered in cell C6

any help would be appreciated

thank you
Dan
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I think this is what you are looking for.

VBA Code:
Sub HideRows()

Dim r As Long
Dim LastRow As Long
LastRow = Cells(Rows.Count, "P").End(xlUp).Row
For r = LastRow To 1 Step -1
If Cells(r, "P") = Range("C6")
Rows(r).EntireRow.Hidden = True
End If
Next r

End Sub
 
Upvote 0
I think this is what you are looking for.

VBA Code:
Sub HideRows()

Dim r As Long
Dim LastRow As Long
LastRow = Cells(Rows.Count, "P").End(xlUp).Row
For r = LastRow To 1 Step -1
If Cells(r, "P") = Range("C6")
Rows(r).EntireRow.Hidden = True
End If
Next r

End Sub
thank you but the code provided hides all the rows that matches the value in column P and value in cell C6 .... I need all rows that does not match the name in cell 6 and value in column P to be hidden
 
Upvote 0
Just changed the = to <>

VBA Code:
Sub HideRows()

Dim r As Long
Dim LastRow As Long
LastRow = Cells(Rows.Count, "P").End(xlUp).Row
For r = LastRow To 1 Step -1
If Cells(r, "P") <> Range("C6")
Rows(r).EntireRow.Hidden = True
End If
Next r

End Sub
 
Upvote 0
How about:
VBA Code:
Sub HideRows()
    Range("P7:P2000").AutoFilter Field:=1, Criteria1:=Range("C6").Value
End Sub
 
Last edited:
Upvote 0
Solution
Just changed the = to <>

VBA Code:
Sub HideRows()

Dim r As Long
Dim LastRow As Long
LastRow = Cells(Rows.Count, "P").End(xlUp).Row
For r = LastRow To 1 Step -1
If Cells(r, "P") <> Range("C6")
Rows(r).EntireRow.Hidden = True
End If
Next r

End Sub
that worked ... thanks

but I forgot to consider that I have info in rows 1 to 7 that MUST remain visible at all time; so only row 8 and onward can be hidden if the criteria is met... I apologize for the omission.
 
Upvote 0
VBA Code:
Sub HideRows()

Dim r As Long
Dim LastRow As Long
LastRow = Cells(Rows.Count, "P8").End(xlUp).Row
For r = LastRow To 1 Step -1
If Cells(r, "P") <> Range("C6")
Rows(r).EntireRow.Hidden = True
End If
Next r

End Sub
 
Upvote 0
VBA Code:
Sub HideRows()

Dim r As Long
Dim LastRow As Long
LastRow = Cells(Rows.Count, "P8").End(xlUp).Row
For r = LastRow To 1 Step -1
If Cells(r, "P") <> Range("C6")
Rows(r).EntireRow.Hidden = True
End If
Next r

End Sub
I added Then at the end of:

If Cells(r, "P") <> Range("C6")

but I still get an error

1692644779428.png

1692644806228.png
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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