使用AutoFitler(VBA)过滤带标准的月份

时间:2016-04-15 18:58:36

标签: excel vba date criteria autofilter

因此从理论上讲,这个按月过滤日期的概念应该有效,但事实并非如此。我使用的表单记录了每个已完成的任务,然后使用标准格式对其进行约会。我很困惑为什么这段代码无法计算这种形式的日期。表格中的每个月将始终格式化为x / xx / xxxx(单个数字月份)和xx / xx / xxxx(两位数月份)。在我看来,找到一个以每个月的字符开头然后计算它们的单元格应该是一个足够简单的概念来满足我的需求。问题是,当它根据此过滤器计算行数时,它会返回0.有人知道发生了什么吗?

谢谢!

 With iTable
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=tin
        .AutoFilter Field:=3, Criteria1:="=1/*"
        End With
        TData.Cells(i, 4).Value = iTable.Resize(, 1).SpecialCells(xlCellTypeVisible).Count - 1


        With iTable
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=tin
        .AutoFilter Field:=3, Criteria1:="=2/*"
        End With
        TData.Cells(i, 5).Value = iTable.Resize(, 1).SpecialCells(xlCellTypeVisible).Count - 1

1 个答案:

答案 0 :(得分:0)

要获得每月的准确计数,您可以使用右侧未使用的列作为MONTH function的“帮助”列。在将“帮助”列用作Range.AutoFilter Method的条件后,可以删除该“帮助”列。

您还可以在C列的日期写一个快速Conditional Formatting,并将Option Explicit Sub Macro2() Dim m As Long, n As Long, tin As String, vMNTHs As Variant vMNTHs = Array(1, 2) tin = "tin" With Worksheets("sheet3") If .AutoFilterMode Then .AutoFilterMode = False With .Cells(1, 1).CurrentRegion For m = LBound(vMNTHs) To UBound(vMNTHs) If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False With .Resize(.Rows.Count - 1, 1).Offset(1, 2) .FormatConditions.Add Type:=xlExpression, Formula1:="=MONTH(C2)=" & vMNTHs(m) .FormatConditions(.FormatConditions.Count).SetFirstPriority .FormatConditions(1).Interior.Color = vbRed .FormatConditions(1).StopIfTrue = True End With .AutoFilter Field:=1, Criteria1:=tin, Operator:=xlFilterValues .AutoFilter Field:=3, Criteria1:=vbRed, Operator:=xlFilterCellColor n = Application.Subtotal(102, Columns(3)) MsgBox "the count for " & Format(DateSerial(2016, vMNTHs(m), 1), "mmmm") & " is " & n With .Resize(.Rows.Count - 1, 1).Offset(1, 2) .FormatConditions(1).Delete End With Next m End With If .AutoFilterMode Then .AutoFilterMode = False End With End Sub 选项应用到C列的.Autofilter。

{{1}}