Macro to filter column based on input box

dfarr

New Member
Joined
Nov 9, 2015
Messages
2
Hello,

I am pretty new to VBA and I am currently trying to make a macro that applies a filter to a certain column based on the text a user enters into an input box. My worksheet has columns with months as headers from cell B2:Z2 in the format mmm'yy (i.e. Nov'15) and data below each month. I want the macro to allow the user to enter a month and year in the same format into an input box then for excel to find and select the column containing that month and year and apply a filter to the data.

This is the code that I currently have but it is giving me an error:

Sub Test()

Dim iPutFound As Range
Dim iPut As String

iPut = Application.InputBox("Select Month", Type:=2)

Set iPutFound = Sheets("Sheet1").Range("B1:Z1").Find(What:=iPut, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)

If Not iPutFound Is Nothing Then
ActiveSheet.Range("$A$1:$Z$9359").AutoFilter Field:=iPutFound, Criteria1:="1"

Else
MsgBox "No match found."
End If

End Sub

Thank you in advance!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The Autofilter Field number is the found column number in your case.

ActiveSheet.Range("$A$1:$Z$9359").AutoFilter Field:=iPutFound.Column, Criteria1:="1"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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