Input box to filter rows

kathleen0927

New Member
Joined
Feb 7, 2018
Messages
32
Hey! Looking for some help with creating an input box to filter an Excel spreadsheet that has about 1900 rows of data.

I want to have a push button control that will produce an input box where the end user will enter data to filter the list. Data will be located in column A starting in row 11. When the user enters info ("ABC1"), the list will be filtered showing only rows with that code in column A. I'd also like the selection entered ("ABC1") to be inserted in my header in cell C4 for reference.

I have never worked with input boxes before so everything I am looking at, is failing miserably!
 
Last edited:

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.
Hi

try following & see if does what you want


Place code in a STANDARD module

Code:
Sub FilterData()
    Dim FilterRange As Range, Rng As Range
    Dim GetInput As Variant
    
    Do
        GetInput = InputBox("Enter Search Criteria", "Search")
'cancel pressed
        If StrPtr(GetInput) = 0 Then Exit Sub
    Loop Until Len(GetInput) > 0
    
    With ThisWorkbook.Worksheets("Sheet1")
'set filter range (sheet must not be protected)
    Set FilterRange = .Range("A11").CurrentRegion
'insert to header in cell C4 for reference.
        .Range("C4").Value = GetInput
    End With
    
    With FilterRange
'clear filter
        .Cells(1, 1).AutoFilter
        .AutoFilter Field:=1, Criteria1:=GetInput, VisibleDropDown:=True
    End With
    
    Set Rng = FilterRange.Parent.AutoFilter.Range
    
'check for matching criteria
    If Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count = 1 Then
'clear filter
       Rng.AutoFilter
'inform user
        MsgBox GetInput & Chr(10) & "Search Value Not Found", 48, "Not Found"
        
    End If
    
End Sub

Change sheet name shown in RED as required

It is assumed that Row 11 is the HEADER row. Also, your worksheet is not protected

Hope helpful

Dave.
 
Last edited:
Upvote 0
Thank you for the quick reply, Dave.

I have a password on the file so I added "ActiveSheet.Unprotect Password:" and "ActiveSheet.Unprotect Password:" code before and after the "If" statements.

Row 11 is where my data starts, with rows 1 - 10 as the header. I changed the Filter range to "A10" and it works except that it also hides rows 9 and 10 of the header. Do you know how I would adjust this so all header rows (1 - 10) remain? Also is there a way to force the enter to CAPS (but still allow numbers) so when it inserts the input value in C4 it will appear like "ABC1" not "abc1"?

Thanks for your help with this!
 
Upvote 0
Hi,
seems like code going in right direction

see if this update helps


Rich (BB code):
Sub FilterData()
    Dim FilterRange As Range, Rng As Range
    Dim GetInput As Variant
    Dim ws As Worksheet
    
'add sheet protect password if required
    Const wsPassword As String = ""
    
'change sheet name as required
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    
    Do
        GetInput = InputBox("Enter Search Criteria", "Search")
'cancel pressed
        If StrPtr(GetInput) = 0 Then Exit Sub
    Loop Until Len(GetInput) > 0
    
    With ws
'unprotect sheet
        .Unprotect Password:=wsPassword
'set filter range
    Set FilterRange = .Range("A1").CurrentRegion
'ignore header rows
    Set FilterRange = FilterRange.Offset(10, 0).Resize(FilterRange.Rows.Count - 10)
'insert to header in cell C4 for reference.
        .Range("C4").Value = UCase(GetInput)
    End With
    
    With FilterRange
'apply filter
        .Rows(1).AutoFilter
        .AutoFilter Field:=1, Criteria1:=GetInput, VisibleDropDown:=True
    End With
    
    Set Rng = FilterRange.Parent.AutoFilter.Range
    
'check for matching criteria
    If Rng.Columns(1).SpecialCells(xlCellTypeVisible).Count = 1 Then
'clear filter
       Rng.AutoFilter
'inform user
        MsgBox GetInput & Chr(10) & "Search Value Not Found", 48, "Not Found"
        
    End If
    
    ws.Protect Password:=wsPassword
End Sub

Dave
 
Upvote 0
I got a run time error 1004 on the following line according to the debugger

Code:
Set FilterRange =FilterRange.Offset(10,0).Resize(FilterRange.Rows.Count -10)
 
Upvote 0
Interesting, worked ok on test file I just created

Did you use the code unaltered as posted or just update existing code?

Dave
 
Upvote 0
I copied/ pasted into a new module. I only added the password to the Const wsPassword As String = "" line. I have been trying to figure it out but i am stumped.

OK - As I was typing this, i looked at the file and realized that there was no data in column A for the header rows. I added dummy data and it worked! Holy Moley!

Thanks so much Dave for all your help. You went above and beyond for me!
 
Upvote 0
OK - As I was typing this, i looked at the file and realized that there was no data in column A for the header rows. I added dummy data and it worked! Holy Moley!

Thanks so much Dave for all your help. You went above and beyond for me!


There is always a reason & good you sorted it.

Glad I was of help & solution does what you want.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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