简体   繁体   English

使用 Array , MS Excel 自动过滤的标准

[英]Criteria for Autofilter using Array , MS Excel

I've created a module to delete rows using Autofilter and Criteria is Min date,我创建了一个模块来使用 Autofilter 和 Criteria is Min date 删除行,

图片1

图2

图3

The problem is AutoFilter doesn't work when I create an array to define criteria.问题是当我创建一个数组来定义条件时,自动筛选不起作用。 Is there anyway to fix this problem.有没有办法解决这个问题。

Sub Delete_Rows()
    Dim lo As ListObject
    Dim arr As Variant
    arr = Sheet4.Range("A2")

    Set lo = Sheet1.ListObjects(1)
    lo.Range.AutoFilter Field:=2, Criteria1:=arr, Operator:=xlFilterValues
    Application.DisplayAlerts = False
    lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
    Application.DisplayAlerts = True
    lo.AutoFilter.ShowAllData
End Sub
  1. You can modifiy your code to get the value of you min date : arr = Sheet4.Range("A2").value您可以修改您的代码以获取您最小日期的值: arr = Sheet4.Range("A2").value
  2. In the filters on Excel, in VBA you have to send the dates in English format and add the condition.在 Excel 的过滤器中,在 VBA 中,您必须以英文格式发送日期并添加条件。 => Criteria1:=">" & Format(arr, "mm/dd/yy") => Criteria1:=">" & 格式(arr, "mm/dd/yy")

Sub Delete_Rows()子 Delete_Rows()

Dim lo As ListObject
Dim arr As Variant
arr = Sheet4.Range("A2").value

Set lo = Sheet1.ListObjects(1)
lo.Range.AutoFilter Field:=2, Criteria1:=">" & Format(arr, "mm/dd/yy"), Operator:=xlFilterValues
Application.DisplayAlerts = False
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
lo.AutoFilter.ShowAllData

End Sub结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM