VBA Table 筛选和刷新

VBA Table filter and refresh

Manual choice使用我从 Internet 修改的代码,我得到了我的 table 正确过滤日期,但是当单击按钮时它不显示任何结果,直到我手动单击过滤器上的确定 VBA 应用后,一旦我这样做,它就会显示正确的结果,我在这里错过了一个技巧吗?

Sub FilterListOrTableData()

    Dim ACell As Range
    Dim ActiveCellInTable As Boolean
    Dim FilterCriteria As String
    Dim Created As String
    Dim sToday As String
    Dim sStartDate As String
    Dim dStartDate As Date
    Dim sUpperBound, sLowerBound As String


    'Check to see if the worksheet is protected.
    If ActiveSheet.ProtectContents = True Then
        MsgBox "This macro will not work when the worksheet is write-protected.", _
               vbOKOnly, "Filter example"
        Exit Sub
    End If

    'Set a reference to the ActiveCell named ACell. You can always use
    'ACell now to point to this cell, no matter where you are in the workbook.
    Set ACell = ActiveCell

    'Test to see if ACell is in a table or list. Note that by using ACell.ListObject, you
    'don't need to know the name of the table to work with it.
    On Error Resume Next
    ActiveCellInTable = (ACell.ListObject.Name <> "")
    On Error GoTo 0

    'If the cell is in a list or table, run the code.
    If ActiveCellInTable = True Then
        'Show all data in the table or list.
        On Error Resume Next
        ActiveSheet.ShowAllData
        On Error GoTo 0

        'This example filters on the first column in the List/Table
        '(change the field if needed). In this case the Table starts
        'in A so Field:=1 is column A, field 2 = column B, ......
        'Use "<>" & filtercriteria if you want to exclude the criteria from the filter.
        'FilterCriteria = InputBox("What text do you want to filter on?", _
                           '       "Type in the filter item.")
      '  ACell.ListObject.Range.AutoFilter _
              '  Field:=1, _

             '  Criteria1:="=" & FilterCriteria
             'This example filters on the ActiveCell value.
'ACell.ListObject.Range.AutoFilter _
 '   Field:=ACell.Column - ACell.ListObject.Range.Cells(1).Column + 1, _
  '  Criteria1:="=" & ACell.Text

    sToday = Date
    dStartDate = DateValue(Date) - 7
    sStartDate = Str(dStartDate)

    sLowerBound = ">=" + sStartDate
    sUpperBound = "<=" + sToday

    ACell.ListObject.Range.AutoFilter Field:=89, Criteria1:=sLowerBound, _
    Operator:=xlOr, Criteria2:=sLowerBound

    ActiveWorkbook.RefreshAll

    'Else
       ' MsgBox "Select a cell in your list or table before you run the macro.", _
               'vbOKOnly, "Filter example"
    End If

End Sub

这是我手动选项时宏记录的内容:

Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveSheet.ListObjects("FTLOG").Range.AutoFilter Field:=84, Criteria1:= _
        ">=29/11/2016", Operator:=xlOr, Criteria2:=">=29/11/2016"
End Sub

问题出在逻辑上而不是AutoFilter


xlOR 将显示满足条件之一的所有记录

情况 1:显示这条记录因为条件 2 为真

  • 条件一:1/1/2016>=2/1/2016 : 假
  • 条件二:1/1/2016<=12/6/2016 : 正确

情况2:显示这条记录是因为条件1和条件2为真

  • 条件一:7/31/2016>=2/1/2016 : 正确
  • 条件二:7/31/2016<=12/6/2016 : 正确

情况3:显示这条记录是因为条件1和条件2为真

  • 条件一:7/31/2016>=2/1/2016 : 正确
  • 条件二:7/31/2016<=12/6/2016 : 正确

情况 3:显示此记录,因为条件 2 为真

  • 条件一:12/31/2016>=2/1/2016 : 正确
  • 条件二:12/1/2016<=12/6/2016 : 正确

xlAND 将显示满足所有条件的所有记录

案例 1:隐藏这条记录,因为条件 1 为假

  • 条件一:1/1/2016>=2/1/2016 : 假
  • 条件二:1/1/2016<=12/6/2016 : 正确

情况2:显示这条记录是因为条件1和条件2为真

  • 条件一:7/31/2016>=2/1/2016 : 正确
  • 条件二:7/31/2016<=12/6/2016 : 正确

情况 3:隐藏这条记录,因为条件 2 为假

  • 条件一:7/31/2016>=2/1/2016 : 正确
  • 条件二:7/31/2016<=12/6/2016 : 正确

情况 3:显示此记录,因为条件 2 为真

  • 条件一:12/31/2016>=2/1/2016 : 正确
  • 条件二:12/1/2016<=12/6/2016 : 正确

Sub FilterOr()
    Range("$A:$F").AutoFilter Field:=2, Criteria1:= _
        ">=" & Range("StartDate"), Operator:=xlOr, Criteria2:="<=" & Range("Today1")
End Sub

Sub FilterAnd()
    Range("$A:$F").AutoFilter Field:=2, Criteria1:= _
        ">=" & Range("StartDate"), Operator:=xlAnd, Criteria2:="<=" & Range("Today1")
End Sub

这个有效:

Option Explicit

Public Sub TestMe()

    Dim dToday          As Date: dToday = Date
    Dim dStartDate      As Date: dStartDate = Date - 7
    Dim dEndDate        As Date: dEndDate = Date

    Dim sLowerBound     As String
    Dim sUpperBound     As String

    dToday = Format(dToday, "dd/mm/yyyy")
    dStartDate = Format(dStartDate, "dd/mm/yyyy")
    dEndDate = Format(dEndDate, "dd/mm/yyyy")

    sLowerBound = ">=" & CLng(dStartDate)
    sUpperBound = "<=" & CLng(dToday)

    ActiveSheet.Range("$A:$B").AutoFilter Field:=2, Criteria1:=sLowerBound, Operator:=xlOr, Criteria2:=sUpperBound

End Sub