【问题标题】:Error non described on a VBA filtering codeVBA 过滤代码中未描述的错误
【发布时间】:2021-01-28 06:02:24
【问题描述】:

所以,我有这段代码应该允许我过滤 G 列(日期)并只显示前一天的日期(如果今天是 10 月 13 日,那么应该只显示 10 月 12 日)。

我的问题是代码有效地应用了过滤器,但即使 10 月 12 日的值确实在列中,也没有显示结果。

这是结果的屏幕截图: Filtered, but no results shown

这是我的代码。当我从 SAP 下载此信息时,日期有点而不是斜线,所以我对它们执行替换(例如从 01.01.2020 到 01/01/2020): 范围(“A1”)。选择

Columns("G:G").Select

Selection.Replace What:=".", Replacement:="/", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

Selection.NumberFormat = "dd/mm/yyyy;@"

Worksheets("Sheet1").Range("A1").AutoFilter Field:=7, Operator:=xlFilterDynamic, Criteria1:=xlFilterYesterday

我希望你们能帮我解决这个问题。

【问题讨论】:

    标签: excel vba date filtering


    【解决方案1】:

    将“日期”字符串转换为日期

    • 将字符串转换为“真实”日期会更好。下面是一个示例,说明如何做到这一点。

    守则

    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 = ThisWorkbook ' The workbook containing this code.
        
        ' Define worksheet.
        Dim ws As Worksheet
        Set ws = wb.Worksheets(wsName)
        
        ' 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.
        ws.Range("A1").AutoFilter Field:=7, _
                                  Operator:=xlFilterDynamic, _
                                  Criteria1:=xlFilterYesterday
    
    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
    

    【讨论】:

    • 感谢您的帮助。快速提问:由于我的宏存储在 Personal 中,我是否应该将 Set ws = wb.Worksheets(wsName) 替换为其他内容?代码暂时在那里中断。
    • ThisWorkbook 表示包含此代码的工作簿,因此它是您的个人工作簿。因此,要么将 ThisWorkbook 更改为 ActiveWorkbook,要么您最好像这样限定工作簿:Set wb = Workbooks("Test.xlsm")
    • 再次感谢。这非常有效。如果您有谈论 Excel 的博客或页面,请告诉我名称。我很乐意检查一下,因为我处于学习的早期阶段。
    猜你喜欢
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2019-11-13
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多