Excel学习笔记002-003:如何进行筛选

时间:2024-03-18 09:21:01

Sub 如何进行自动筛选()
   Range("A1").AutoFilter Field:=1, Criteria1:="2" \'按第1列(字段),条件为第2列中等于2的进行筛选.
   ActiveCell.AutoFilter  \'取消自动筛选,即后面不加任何条件,则为取消自动筛选.
End Sub

Sub 如何进行自定义筛选()
    Range("A1").AutoFilter field:=2, Criteria1:=">=10",Operator:=xlAnd, Criteria2:="<=40" \'这句可以实现按条件筛选,但最多也就是按两个条件.现在展示的是按单条件来进行筛选.
End Sub

Sub 如何把筛选后的结果放在不同工作簿中()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    For i = 1 To 20
        Worksheets("sheet46").Activate
        Selection.AutoFilter
        Selection.AutoFilter field:=2, Criteria1:=Str(i)
        Cells.Select
        Selection.Copy
        Workbooks.Add
        ActiveSheet.Paste
        Cells.Select
        Selection.Columns.AutoFit
        ActiveWorkbook.SaveAs Filename:="e:xiehui" & Format(Str(i)) & ".xls"
        Range("A1").Activate
        ActiveWorkbook.Close
    Next i
    Range("A1").AutoFilter
    Range("A1").Select
    Application.ScreenUpdating = True
End Sub

Sub 如何把筛选后的结果放在不同工作表中()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Workbooks.Open "e:Excel2008级学生花名册.xls"
    Range("A1").Activate
    For i = 1 To 20
        Worksheets("sheet1").Activate
        Range("A1").AutoFilter
        Range("A1").AutoFilter Field:=2, Criteria1:=Str(i)
        Cells.Select
        Selection.Copy
        Worksheets.Add
        Range("A1").PasteSpecial
        Cells.Select
        Selection.Columns.AutoFit
    Next i
    Worksheets("sheet1").Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub

Sub 如何把筛选后的结果放在不同工作表中改进()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Workbooks.Open "e:Excel2008级学生花名册.xls"
    Range("A1").Activate
    For i = 1 To 20
        Worksheets("sheet1").Activate
        Range("A1").AutoFilter
        Range("A1").AutoFilter Field:=2, Criteria1:=Str(i)
        Range("A1").CurrentRegion.Copy
        Worksheets.Add
        Range("A1").PasteSpecial
        Application.CutCopyMode = False \'这行命令最关键,可以直接解决复制数据过多,导致内在不够的问题!!!
        Cells.Select
        Selection.Columns.AutoFit
    Next i
    Worksheets("sheet1").Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub

反思:对比后两段程序可以发现,及时把粘贴板内的内容清掉是非常关键的,这也是我第一次遇到内存不足的情况!!!

菊子曰 今天你菊子曰了么?