Hide rows that contain two strings or numbers in a cell

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I am having issues with getting a task accomplished.
I have found some scripts on the net but they all seem to get me just half way through the journey. They could find just one string .

In a column say column C, I have data in the form 00-01-02-03-04 and I want to find two of the sub stings either from values in two cells or an InputBox (whichever is cool). Say "00" and "03" (Both must be located not just one). I am not sure how excel is treating those 00 and 03 etc so I am using "string" .

So when those matches are found, hide all rows except those matched.

Else display a message box "No Match Found"

I just hope this is achievable.

Thanks in advance
Kelly
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Kelly,

You might consider the following...

Code:
Sub AnotherLoop_1063452()
Dim r As Range, rng As Range, rng1 As Range, b As Boolean
Dim arr As Variant
Application.ScreenUpdating = False

Set rng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
b = False
arr = Split(Application.InputBox("Please enter two 2-digit numbers separated by a comma."), ",")
If UBound(arr) <> 1 Then
    MsgBox "Your entry is invalid. Please try again."
    Exit Sub
End If

For Each r In rng
    If InStr(r.Value, arr(0)) > 0 And InStr(r.Value, arr(1)) > 0 Then
        b = True
        On Error GoTo nxt
        Set rng1 = Application.Union(rng1, r)
    End If
Next r
If b = True Then
    rng.Rows.Hidden = True
    rng1.EntireRow.Hidden = False
    ActiveWindow.ScrollRow = rng1.Row
Else
    MsgBox "No match found."
End If

Exit Sub
nxt:
    Set rng1 = r
    On Error GoTo 0
    Resume Next
End Sub

Cheers,

tonyyy
 
Upvote 0
Kelly,

You might consider the following...

Code:
Sub AnotherLoop_1063452()
Dim r As Range, rng As Range, rng1 As Range, b As Boolean
Dim arr As Variant
Application.ScreenUpdating = False

Set rng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
b = False
arr = Split(Application.InputBox("Please enter two 2-digit numbers separated by a comma."), ",")
If UBound(arr) <> 1 Then
    MsgBox "Your entry is invalid. Please try again."
    Exit Sub
End If

For Each r In rng
    If InStr(r.Value, arr(0)) > 0 And InStr(r.Value, arr(1)) > 0 Then
        b = True
        On Error GoTo nxt
        Set rng1 = Application.Union(rng1, r)
    End If
Next r
If b = True Then
    rng.Rows.Hidden = True
    rng1.EntireRow.Hidden = False
    ActiveWindow.ScrollRow = rng1.Row
Else
    MsgBox "No match found."
End If

Exit Sub
nxt:
    Set rng1 = r
    On Error GoTo 0
    Resume Next
End Sub

Cheers,

tonyyy

Hi,

Can the match rows be copied to a new sheet instead of hiding them?

If yes then I will be glad you show me how to do that.

Please let me know if I need a new thread for this
 
Upvote 0
Code:
Sub AnotherLoop_1063452()
Dim r As Range, rng As Range, rng1 As Range, b As Boolean
Dim arr As Variant
Application.ScreenUpdating = False

Set rng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
b = False
arr = Split(Application.InputBox("Please enter two 2-digit numbers separated by a comma."), ",")
If UBound(arr) <> 1 Then
    MsgBox "Your entry is invalid. Please try again."
    Exit Sub
End If

For Each r In rng
    If InStr(r.Value, arr(0)) > 0 And InStr(r.Value, arr(1)) > 0 Then
        b = True
        On Error GoTo nxt
        Set rng1 = Application.Union(rng1, r)
    End If
Next r
If b = True Then
'    rng.Rows.Hidden = True
'    rng1.EntireRow.Hidden = False
'    ActiveWindow.ScrollRow = rng1.Row
    rng1.EntireRow.Copy
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Paste
Else
    MsgBox "No match found."
End If

Exit Sub
nxt:
    Set rng1 = r
    On Error GoTo 0
    Resume Next
End Sub
 
Upvote 0
If I understand this properly, it means the code will create a new sheet and paste it inside as that becomes the new activesheet.

I will want to specify a sheet say Sheet3.

Regards
Kelly
 
Upvote 0
Can the match rows be copied to a new sheet instead of hiding them?

So... you asked for a new sheet.

If you want to specify a sheet...

Code:
    rng1.EntireRow.Copy
    Sheets("Sheet3").Activate
    ActiveSheet.Paste
 
Upvote 0
The copying is done but it does not paste it to the sheet3 . Which code should I turn off for the error handling?

So I can see and report what's exactly happening?
 
Upvote 0
Okay just found out what's happening :

The insertion point must must always be in column A since we're copying entire rows .

How do I get that always in cell A1?
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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