Using data in Column for criteria in Filter (VBA)

SToRIB

New Member
Joined
Mar 21, 2012
Messages
3
Hi everyone,

First time poster, long time follower. I have been struggling hard with a particular VBA code.

I'm trying to take a column of data on a sheet (sheet1) and use it as filter criteria for a specific column on another sheet (Q1 2009) within the same workbook. I have code below that will cycle through each value with in the column and enter it into the criteria for the column on the filter sheet but it never retains the filter and just jumps to the next criteria so that the end result is Q1 2009 sheet is filtered by only the value from the last value within the column.

Can someone assist me in having the criteria the filter should use from worksheet "Sheet1" filter the data on "Q1 2009" worksheet? What I have so far is below:

Code:
Sub FilterListOrTableData()
    Dim ACell As Range, ARange As Range
    Dim FilterCriteria As String
    
    Application.ScreenUpdating = False

    'Check to see if the worksheet is protected.
    If ActiveSheet.ProtectContents = True Then
        MsgBox "This macro will not work when the worksheet is write-protected.", _
               vbOKOnly, "Filter example"
        Exit Sub
    End If

    Set ARange = Worksheets("Sheet1").Range("A1:A316")
    For Each ACell In ARange
    Worksheets("Q1 2009").ListObjects("Table1").Range.AutoFilter Field:=6, Criteria1:=ACell.Text
    Next ACell
    
    
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
It's not clear what you want to do each time the data is filtered? After you filter the data, you need to do something with it.

Code:
    [color=darkblue]For[/color] [color=darkblue]Each[/color] ACell [color=darkblue]In[/color] ARange
    Worksheets("Q1 2009").ListObjects("Table1").Range.AutoFilter Field:=6, Criteria1:=ACell.Text
        [color=green]'Do somtheing with each filtered data here[/color]
        [color=green]'[/color]
        [color=green]'[/color]
    [color=darkblue]Next[/color] ACell
 
Upvote 0
I apologize for not being more detailed.

I want the criteria to be saved so that when the macro is finished, all the values from the column on sheet1 are the criteria for the filter on "Q1 2009."

Basically, I want the macro to go through column F on "q1 2009" and "check the box" on values that are in the column on sheet1.
 
Upvote 0
Try this. I don't have the proper version of Excel to test this. It's likely their may be something wrong in the code, but maybe it will point you in the right direction. I found the code syntax here. http://blog.contextures.com/archives/2010/12/15/excel-autofilter-with-criteria-in-a-range/

I suspect if there is a value in the criteria range that is not in the filter list, the code would error.

Code:
Sub FilterListOrTableData()

    Dim vCrit As Variant
    
    'Check to see if the worksheet is protected.
    If ActiveSheet.ProtectContents = True Then
        MsgBox "This macro will not work when the worksheet is write-protected.", _
               vbOKOnly, "Filter example"
        Exit Sub
    End If

    vCrit = Worksheets("Sheet1").Range("A1:A316").Value
    Worksheets("Q1 2009").ListObjects("Table1").Range.AutoFilter Field:=6, _
                                                                 Criteria1:=Application.Transpose(vCrit), _
                                                                 Operator:=xlFilterValues
    
    
End Sub
 
Upvote 0
Thanks for the quick response, but it didn't look like anything was showing up in the filter, either -- even though I know some of the values on Sheet1 exist on the table on "q1 2009". Any other tricks up your sleeve?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,325
Members
449,154
Latest member
pollardxlsm

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