【问题标题】:Script not filtering by last three days脚本在过去三天内未过滤
【发布时间】:2021-08-30 08:02:26
【问题描述】:

我调整了用于解决此处发布的问题的代码How to do filter in VBA for the last three days?,它确实有点帮助。

今天是 6 月 14 日星期一,脚本应该过滤日期以在结果中包含 6 月 11 日星期五。由于某种原因,这并没有发生。我不知道如何修复它,因为这条线对我来说似乎很完美。

任何人都可以发现问题吗?

Option Explicit

Sub convertStringsToDate()
    
    Const wsName As String = "Sheet1"
    Const ColumnIndex As Variant = "G"
    Const FirstRow As Long = 2
    
    ' Define workbook.
    Dim wb As Workbook
    Set wb = Workbooks("Daily Invoiced ZAMSOTC02 LAC TEAM.xlsm") ' The workbook containing this code.
    
    ' Define worksheet.
    Dim ws As Worksheet
    Set ws = wb.Worksheets("Sheet 1")
    
    ' Turn off AutoFilter.
    If ws.AutoFilterMode Then
        ws.AutoFilterMode = False
    End If
    
    ' Define Column Range.
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, ColumnIndex).End(xlUp).Row
    Dim rng As Range
    Set rng = ws.Range(ws.Cells(FirstRow, ColumnIndex), _
                       ws.Cells(LastRow, ColumnIndex))
    
    ' Write values from Column Range to Data Array.
    Dim Data As Variant
    If rng.Rows.Count > 1 Then
        Data = rng.Value
    Else
        ReDim Data(1 To 1, 1 To 1)
        Data(1, 1) = rng.Value
    End If
    
    ' Convert values in Data Array, converted to strings, to dates.
    Dim CurrentValue As Variant
    Dim i As Long
    For i = 1 To UBound(Data)
        CurrentValue = DotToSlashDate(CStr(Data(i, 1)))
        If Not IsEmpty(CurrentValue) Then
            Data(i, 1) = CurrentValue
        End If
    Next
    
    ' Write dates from Data Array to Column Range.
    rng.Value = Data
       
    ' Apply AutoFilter.
    
        Dim iCol As Range
        Set iCol = rng

    Debug.Print CLng(DateAdd("d", 0, Date))
    
        If Weekday(Now()) = vbMonday Then
        ws.Range("A1").AutoFilter Field:=7, Criteria1:=">" & CLng(DateAdd("d", -3, Date)), Criteria2:="<>" & CLng(DateAdd("d", 0, Date))
    Else
        ws.Range("A1").AutoFilter Field:=7, Operator:=xlFilterDynamic, Criteria1:=xlFilterYesterday
    End If
                               
End Sub

' Converts a string in the format of either d.m.yyyy or d.m.yyyy.
' to a date in the current Excel date format.
' If the string is not in the required format, it returns empty.
Function DotToSlashDate(DotDate As String) As Variant
    On Error GoTo ProcExit
    Dim fDot As Long
    fDot = InStr(1, DotDate, ".")
    Dim dDay As String
    dDay = Left(DotDate, fDot - 1)
    Dim sDot As Long
    sDot = InStr(fDot + 1, DotDate, ".")
    Dim mMonth As String
    mMonth = Mid(DotDate, fDot + 1, sDot - fDot - 1)
    Dim yYear As String
    yYear = Replace(Right(DotDate, Len(DotDate) - sDot), ".", "")
    DotToSlashDate = DateSerial(CLng(yYear), CLng(mMonth), CLng(dDay))
ProcExit:
End Function

【问题讨论】:

    标签: vba date filtering


    【解决方案1】:

    请试试这个方法:

    ws.Range("A1").AutoFilter Field:=7, Criteria1:=">" & CLng(Date - 3), Criteria2:="<=" & CLng(Date)
    

    我们是否应该理解您的初始范围是文本,而不是日期?

    如果是,您可以维护您的代码以转换日期中的文本,或者使用更简单的函数:

    Function TextToDate(strText As String) As Date
       Dim arrD: arrD = Split(strText, ".")
       
       TextToDate = DateSerial(arrD(2), arrD(1), arrD(0))
    End Function
    

    上述函数只需要格式为“dd.mm.yyyy”的文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2022-06-10
      • 2017-03-04
      • 1970-01-01
      • 2018-10-22
      • 2020-08-25
      • 2021-08-25
      相关资源
      最近更新 更多