Search for and hide many rows based on value of 2 cells

Loin75

Active Member
Joined
Oct 21, 2009
Messages
281
Hi - the title is probably not very accurate, sorry.


I have a spreadsheet with the following:
Column A = Client Name
Column B = Postcode
Column C = Numerical Values


John Smith (for example) may be entered in many rows. Lets say he has 10 rows with respective data.
Column C should be numerical, but there are "?" entered for some values that we do not know.


My goal - If any one of John's rows has a ? entered into Column C, then I want to hide all of John's rows.


To make this slightly more complicated, we need to be able to match John Smith with his postcode to make sure we are not hiding rows belonging to a different John Smith. (we are talking 20000 rows of client data).


I have this code as my starter, but do not have the knowledge to include the correct search mechanism, if it is at all possible.

Code:
Option Explicit
 
Dim c As Range, rng As Range
 
Sub Hide()
     
    Set rng = Range("C2", Range("C10000").End(xlUp))
     
    For Each c In rng
         
        If c.Value = "?" Then
             
            c.EntireRow.Hidden = True
             
        End If
         
    Next cel
     
End Sub



Many thanks for any help...
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
What version of Excel are you using?
 
Upvote 0
maybe something like this...

Sub Hide()
rng = Range("C10000").End(xlUp).Row
Dim MyArray()
ReDim MyArray(1 To rng - 1, 1 To 2)
Z = 0
For i = 2 To rng
If Range("C" & i).Value = "?" Then
Z = Z + 1
MyArray(Z, 1) = Range("A" & i).Value
MyArray(Z, 2) = Range("B" & i).Value
End If
Next i
For j = 2 To rng
x = Range("A" & j).Value
y = Range("B" & j).Value
For k = 1 To Z
If x = MyArray(k, 1) And y = MyArray(k, 2) Then
Range("A" & j).EntireRow.Hidden = True
End If
Next k
Next j

End Sub
 
Upvote 0
Here's the code I came up with:
Code:
Sub MyHide()


    Dim myStartRow As Long
    Dim myLastRow As Long
    Dim myRow As Long
    Dim RangeA As Range
    Dim RangeB As Range
    Dim RangeC As Range
    Dim myCount As Long
    
'   Set row that data starts on
    myStartRow = 2
        
'   Find last row with data in column A
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Set ranges
    Set RangeA = Range("A" & myStartRow & ":A" & myLastRow)
    Set RangeB = Range("B" & myStartRow & ":B" & myLastRow)
    Set RangeC = Range("C" & myStartRow & ":C" & myLastRow)
    
    Application.ScreenUpdating = False
    
'   Loop through all rows
    For myRow = myStartRow To myLastRow
'       Count number of question marks in all data for criteria on particular row
        myCount = Application.WorksheetFunction.CountIfs(RangeA, Cells(myRow, "A"), RangeB, Cells(myRow, "B"), RangeC, "?")
'       If any found, hide row
        If myCount > 0 Then Rows(myRow).EntireRow.Hidden = True
    Next myRow
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thanks Joe4 - that code is perfect. I am going to study it for a while, you legend!!

Thanks also to Joemeshuggah
 
Upvote 0
Thanks Joe4 - that code is perfect. I am going to study it for a while, you legend!!
You're welcome. If you have any specific questions about it, feel free to ask them.
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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