Filter a column using a cell value?

nonono

Board Regular
Joined
Jul 25, 2018
Messages
59
Example If i were to input a value '240*' in Cell "A1", it will auto filter another column in another sheet column D

Is it possible?

I would like to use the cell to filter instead of using the filter option?
Would be great if you guys can come out with a script.
Thank you in advance and and thank you reading this post and
 

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.
Firstly, yes. It's relatively straightforward once you know the filter code. However it would help to give accurate code if you provided:

  1. the name of the sheet the user will enter a value in to Cell A1
  2. Name of sheet you will be filtering
  3. range you are filtering
  4. will the range vary every time or is it static?
 
Upvote 0
As a reference point, this, although it can be greatly simplified shows the process. This code works from a module and relies on you changing the ranges and sheet names to suit yours.

Code:
Public Sub FilterRange()


    Dim wsFilter As Worksheet 'Worksheet to filter
    Dim wsCriteria As Worksheet 'Worksheet With the criteria
    
    Dim Filter_Cell As Range 'Cell with user input
    Dim Filter_Range As Range 'Range to filter
    
    Dim sCriteria As String 'Variable to hold filter string
    
    Dim lr As Long ' Last Row for filtering
    


    'Set worksheets. Change names accordingly
    Set wsFilter = Worksheets("Sheet2")
    Set wsCriteria = Worksheets("Sheet1")
    
    'Get last row
    lr = wsFilter.Range("D" & wsFilter.Rows.Count).End(xlUp).Row
    
    'Set range objects
    Set Filter_Cell = wsCriteria.Range("A1")
    Set Filter_Range = wsFilter.Range("A1:D" & lr) 'Change this range to suit
    
    'store the filter string
    sCriteria = Filter_Cell.Value
    
    'Filter the range
    Filter_Range.AutoFilter 'clear previous
    
    'If we have some criteria, filter on it
    If sCriteria <> "" Then Filter_Range.AutoFilter Field:=4, Criteria1:=sCriteria
    wsFilter.Activate
    
End Sub

To activate the code you check the user has altered the A1 cell and then call this sub. This code goes in the worksheet with the criteria cell...

Private Sub Worksheet_Change(ByVal Target As Range)
'If user has altered filter then run filter sub
If Target.Address = Range("A1").Address Then FilterRange
End Sub
 
Upvote 0
Alternatively just post this code in to the sheet with the criteria cell (A1 in your OP)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Filter_Range As Range
    
    If Target.Address = Range("A1").Address Then
        
        'clear filter
        Worksheets("Sheet2").UsedRange.AutoFilter
        
        'Assuming the header row is row 1
        Set Filter_Range = Worksheets("Sheet2").Range("A1:D" & Worksheets("Sheet2").Range("D" & Worksheets("Sheet2").Rows.Count).End(xlUp).Row)
        
        'Apply filter if there is one
        If Target <> "" Then Filter_Range.AutoFilter Field:=4, Criteria1:=Target.Value
        Worksheets("Sheet2").Activate
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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