Searching a Range For Two Known Text Values and Hiding the Rows in between.

redelmab

New Member
Joined
May 28, 2010
Messages
4
Hello,

I am a beginner user of VBA and am having trouble finding a function to do what I need. I have a predefined range in excel (about 1000 cells) called Range 1. In this range I have the values "user" and "visitor". When a checkbox (chk1) is checked, I want a code to search Range 1 for the text value "user" and hide the rows between it and "visitor". I do not want user and visitor to be hidden though. Any help is GREATLY appreciated :)!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
This will hide everything between the first and last

Pseudocode: (Meaning, you have to flesh it out.)
Code:
'first, unhide all rows
.Rows.Hidden = False 
Dim R As Long, _
First As Long, _
Last As Long, _
Visitor As String
 
Visitor = InputBox("Pls enter the visitor to show")
 
First = .Find(Visitor lookin:=xlValues).Row
Last = .FindNext.Row
 
If First > Last Then Exit Sub 'It looped around
 
For R = First + 1 To Last - 1
   Rows(R).Hidden = True
Next R

This will hide everything except the First and Last
Code:
.Rows.Hidden = True 'Hide all
Rows(First).Hidden = False 'Show First
Rows(Last).Hidden = False 'Show Last
 
Upvote 0
Or:

Code:
Sub test()
    Rows(Range("range 1").Find("user", , xlValues, xlWhole).Row + 1, Range("range 1").Find("visitor").Row - 1).Hidden = True
End Sub
 
Upvote 0
Hello Wigi,
I have tried to use your code but when I do I get an application define or object define error.
This is what my code looke like:

Private Sub butContinue_Click()
'This will hide rows when a checkbox is selected.
If Me.Chkbox1.Value = True Then

Rows(Worksheets("Sheet 1").Range("Chosen").Find("user", , xlValues, xlWhole).Row + 1, Worksheets("Sheet 1").Range("Chosen").Find("visitor").Row - 1).Hidden = True

End If
End Sub

Any ideas as to what is wrong?
 
Upvote 0
Try...

Code:
Rows(Worksheets("Sheet 1").Range("Chosen").Find("user", , xlValues, xlWhole).Row + 1[COLOR=red] & ":" &[/COLOR] Worksheets("Sheet 1").Range("Chosen").Find("visitor").Row - 1).Hidden = True
 
Upvote 0
Thanks!!!! It worked. One other addendum. Say I found user, and only wanted to find the visitor term lower than user in the spreadsheet. At the moment the sub will find the first value it comes too...not the first value after the user cell is found. Any helpful ideas?

Thanks Again!
 
Upvote 0
Try...

Code:
Option Explicit
Sub test()
 
    Dim rUser As Range, rVisitor As Range
    Dim UserRow As Long, VisitorRow As Long
 
    With Worksheets("Sheet 1").Range("Chosen")
        .Parent.Rows.Hidden = False
        Set rUser = .Find("user", , xlValues, xlWhole)
        If Not rUser Is Nothing Then
            UserRow = rUser.Row
        Else
            MsgBox "No user row exists...", vbInformation
            Exit Sub
        End If
        Set rVisitor = .Find("visitor", rUser, xlValues, xlWhole)
        If Not rVisitor Is Nothing Then
            VisitorRow = rVisitor.Row
        Else
            MsgBox "No visitor row exists...", vbInformation
            Exit Sub
        End If
        If UserRow > VisitorRow Then
            MsgBox "No visitor row exists after the user row...", vbInformation
        ElseIf VisitorRow - UserRow = 1 Then
            MsgBox "No rows exist between the user row and visitor row...", vbInformation
        Else
            .Parent.Rows(UserRow + 1 & ":" & VisitorRow - 1).Hidden = True
        End If
    End With
 
End Sub
 
Upvote 0
Thanks Domenic. I couldnt get the second solution of yours to work, but I can modify my spreadsheet so that the original code you posted works everytime. Thank You!
 
Upvote 0
Thanks Domenic. I couldnt get the second solution of yours to work, but I can modify my spreadsheet so that the original code you posted works everytime. Thank You!

If you're still interested in pursuing the last solution I offered, describe what happens when you run the code. Does an error occur? If so, which one, and which line gets highlighted?
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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