Lỗi activeworkbook.worksheets tô ng2 sort.sortfields.clear

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 `ThisWorkBook`e.g.

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

Sub Macro4[]

Cells.Select  
Selection.AutoFilter  
End Sub Sub Macro5[]
ActiveSheet.Range["$A$1:$L$38"].AutoFilter Field:=8, Criteria1:=Array[ _  
    "ALESSANDRO", "MARCO", "ROBERTO "], Operator:=xlFilterValues  
End Sub Sub Macro6[]
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

Sub MergeMacros[]

Cells.Select  
Selection.AutoFilter  
ActiveSheet.Range["$A$1:$L$38"].AutoFilter Field:=8, Criteria1:=Array[ _  
"ALESSANDRO", "MARCO", "ROBERTO "], Operator:=xlFilterValues  
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

Sub MergedMacroMadeExplicit[]

ThisWorkbook.Sheets[1].Cells.Select  
Selection.AutoFilter  
ThisWorkbook.Sheets[1].Range["$A:$L"].AutoFilter Field:=8, Criteria1:=Array[ _  
    "ALESSANDRO", "MARCO", "ROBERTO "], Operator:=xlFilterValues      
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

Sub MyMacroForPuttingAFilterOn[]

With ThisWorkbook.Sheets[1]  
    .Cells.AutoFilter  
    .Range["$A:$L"].AutoFilter Field:=8, Criteria1:=Array["ALESSANDRO", "MARCO", "ROBERTO "], Operator:=xlFilterValues  
    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

Chủ Đề