Filter a Worksheet VBA

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
961
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Happy Holidays everyone,

First I want to thank everyone for all your help. Everyone here at MrExcel are very kind with their time helping others.

My question is the following:
I have a worksheet that has a number of columns with various data.
The cell columns I would like to filter are columns A through E.
Each of these columns have numbers that range from 1 through 36.
Is there a way to filter numbers for example in cell column A where I can have a message box or other method where I can just enter 5 of the numbers from 1 through 36 that I want to filter.
I know I can actually do the filtering on the worksheet that has all the filters. I was looking for a way to do this from another worksheet.

Thank you in advance!!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The following sample code with the comments might help.

VBA Code:
Sub doFilterByUserEntry()
Dim sht As Worksheet
Dim rng As Range
Dim strColumn As String
Dim strIDs As String

    ' The worksheet and the data range
    Set sht = Sheet1
    Set rng = sht.Range("A1").CurrentRegion

    ' Ask the column - default is presented as column A
    strColumn = InputBox("Which column?", "Select column", "A")
    ' Simple validation to make sure if the user didn't cancel the input box
    ' Further validation could be applied to check if user entered a correct column letter
    If strColumn = "" Then
        Exit Sub
    End If
    ' Set the selected column
    Set rngcol = sht.Range(strColumn & ":" & strColumn)
    
    ' If the selected column is not in the data range then we can't continue
    If Application.Intersect(rng, rngcol) Is Nothing Then
        MsgBox "Please select a column in the data range.", vbOKOnly + vbExclamation, "Invalid column"
        Exit Sub
    End If
    
    ' Ask the numbers
    strIDs = InputBox("Enter IDs to filter data, use either space or comma to separate numbers", "User entry")
    strIDs = Replace(strIDs, ",", " ")
    
    ' If the data range is already filtered, then clear the previously applied filter
    If sht.AutoFilterMode Then
        sht.ShowAllData
    End If
    
    ' Filter the data range by the requested column and the numbers
    rng.AutoFilter rngcol.Column, Split(strIDs, " "), xlFilterValues
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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