Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Goodmorning everyone,
For the Automatic filter with words containing example MARCO, ALESSANDRO, ROBERTO on the column from H2: H100 and column L (Sort from oldest to most recent)

How can I do?

Thanks everyone for the help

What I have tried:

'========>>
Option Explicit

'-------->>
Public Sub Tester1()
Range("A1:T30000").Sort Key1:=Range("L1"), Order1:=xlDescending, Header:=xlYes
Range("A2:T30000").Select
End Sub
'<<========
Posted
Updated 14-Dec-21 0:37am

1 solution

I have already essentially answered this on your earlier question Import VBA values..................................[^]

Use the macro recorder to find out how to put the filter on and the sort then call that macro from the Workbook_Open method for ThisWorkBooke.g.
VB
Private Sub Workbook_Open()
    MyMacroForPuttingAFilterOn
End Sub


EDIT : Here is a fully worked example

Step 1: Record the steps you need. My advice is to break this down into individual steps rather than attempting everything at once. That way if something goes wrong during the recording you don't have to start right at the beginning. These are the macros I recorded
VB
Sub Macro4()
'
' Macro4 Macro
'

'
    Cells.Select
    Selection.AutoFilter
End Sub
Sub Macro5()
'
' Macro5 Macro
'

'
    ActiveSheet.Range("$A$1:$L$38").AutoFilter Field:=8, Criteria1:=Array( _
        "ALESSANDRO", "MARCO", "ROBERTO "), Operator:=xlFilterValues
End Sub
Sub Macro6()
'
' Macro6 Macro
'

'
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key:= _
        Range("H1:H38"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Step 2:Reset your sheet back to its original state. Merge those macros into one and add some comments. Test that this works
VB
Sub MergeMacros()
    
    'Set Autofilter
    Cells.Select
    Selection.AutoFilter
    
    'Filter on the names required
    ActiveSheet.Range("$A$1:$L$38").AutoFilter Field:=8, Criteria1:=Array( _
    "ALESSANDRO", "MARCO", "ROBERTO "), Operator:=xlFilterValues
    
    'Sort on column H
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key:= _
        Range("H1:H38"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Step 3:Reset your sheet back to it's original state. Tidy up the macro removing default values and making the ranges more generic. Test that this works
VB
Sub MergedMacroMadeExplicit()

    'Set Autofilter
    ThisWorkbook.Sheets(1).Cells.Select
    Selection.AutoFilter
    
    'Filter on the names required
    ThisWorkbook.Sheets(1).Range("$A:$L").AutoFilter Field:=8, Criteria1:=Array( _
        "ALESSANDRO", "MARCO", "ROBERTO "), Operator:=xlFilterValues    'NB the Operator is still required here
    
    'Sort column H
    ThisWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
    ThisWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key:=Range("H:H")
    ThisWorkbook.Worksheets("Sheet1").AutoFilter.Sort.Apply
End Sub
Step 4: Reset your sheet back to its original state. Final tidy up of the VBA. I like to use With statements - it means the interpreter only has to evaluate the expression ThisWorkbook.Worksheets("Sheet1") once
VB
Sub MyMacroForPuttingAFilterOn()

    With ThisWorkbook.Sheets(1)
    
        'Set Autofilter
        .Cells.AutoFilter
        
        'Filter on the names required
        .Range("$A:$L").AutoFilter Field:=8, Criteria1:=Array("ALESSANDRO", "MARCO", "ROBERTO "), Operator:=xlFilterValues
        
        'Sort on Column H
        With .AutoFilter.Sort
            .SortFields.Clear
            .SortFields.Add2 Key:=Range("H:H")
            .Apply
        End With
    
    End With

End Sub
Step 5:Add code to the workbook to call this VBA - see original solution above.
Step 6:Tidy up.
- Delete the previous macros Macro4, Macro5, Macro6 (your names are likely to be different), MergeMacros and MergedMacroMadeExplicit. Don't do it before this step in case you need to take a step back.
- Rename Module1 to be something more meaningful like AutoFilterCode
- Run Debug, Compile VBA Project to make sure there are no errors
- Save your workbook

As you are running this on Workbook_Open you might need to make sure that the AutoFilter is not set whenever you save this workbook
 
Share this answer
 
v3
Comments
LOGOS ITALIASRL 14-Dec-21 6:50am    
Sub Macro1()
'
' Macro1 Macro
'

'
ActiveWorkbook.Worksheets("Scadenziari Scaduti").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Scadenziari Scaduti").AutoFilter.Sort.SortFields. _
Add Key:=Range("L1:L6921"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Scadenziari Scaduti").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.Range("$A$1:$AC$6921").AutoFilter Field:=8, Criteria1:=Array( _
"AMAROLI ALESSANDRO", "BRAVI KAY", "BUTTA ALESSANDRA", "CARTA MASSIMILIANO", _
"CAVAGNA ROBERTA", "FORESTI VALENTINA", "GRIGIS ROBERTO", "MAZZOLA MARZIA"), _
Operator:=xlFilterValues
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900