
excel
VBASub 自动筛选() Dim ws As Worksheet Dim rng As Range Dim filterRange As Range ' 设置工作表 Set ws = ThisWorkbook.Sheets("Sheet1") ' 设置筛选范围 Set rng = ws.Range("A1").CurrentRegion rng.AutoFilter ' 设置筛选条件 With rng .AutoFilter Field:=2, Criteria1:="男性" ' 性别为“男性” .AutoFilter Field:=3, Criteria1:=">25" ' 年龄大于25岁 .AutoFilter Field:=4, Criteria1:=">5000" ' 工资大于5000 End With ' 获取筛选结果 Set filterRange = rng.SpecialCells(xlCellTypeVisible) ' 复制筛选结果到新的工作表 filterRange.Copy Destination:=ThisWorkbook.Sheets("Sheet2").Range("A1") ' 关闭筛选 rng.AutoFilter ' 清除剪贴板内容 Application.CutCopyMode = False MsgBox "筛选完成!"End Sub步骤四:运行 VBA 代码点击运行按钮或按下“F5”键,即可运行 VBA 代码。在运行过程中,VBA 会根据我们设置的条件对数据进行筛选,并将筛选结果复制到新的工作表中。通过使用 excel VBA,我们可以方便地实现对包含多个条件的数据进行自动筛选的功能。只需要编写简单的 VBA 代码,就可以快速完成复杂的筛选任务。希望本文对您在 excel 数据处理中的工作有所帮助。以上就是关于 excel VBA 自动筛选包含多个条件的文章内容,希望能够对读者有所启发和帮助。如果您有任何问题或建议,请随时向我们提问。感谢您的阅读!参考代码:VBASub 自动筛选() Dim ws As Worksheet Dim rng As Range Dim filterRange As Range ' 设置工作表 Set ws = ThisWorkbook.Sheets("Sheet1") ' 设置筛选范围 Set rng = ws.Range("A1").CurrentRegion rng.AutoFilter ' 设置筛选条件 With rng .AutoFilter Field:=2, Criteria1:="男性" ' 性别为“男性” .AutoFilter Field:=3, Criteria1:=">25" ' 年龄大于25岁 .AutoFilter Field:=4, Criteria1:=">5000" ' 工资大于5000 End With ' 获取筛选结果 Set filterRange = rng.SpecialCells(xlCellTypeVisible) ' 复制筛选结果到新的工作表 filterRange.Copy Destination:=ThisWorkbook.Sheets("Sheet2").Range("A1") ' 关闭筛选 rng.AutoFilter ' 清除剪贴板内容 Application.CutCopyMode = False MsgBox "筛选完成!"End SubCopyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号