使用过滤器显示某个范围内的空白单元格数

时间:2016-02-10 21:20:12

标签: excel vba excel-vba

我使用条件格式显示空白单元格,并通过按工作表上有空白的每个人的名称进行过滤来创建宏。现在我需要显示每个人的空白单元格数。我该怎么做呢。

Sub Displayblanks_John_Doe()
 '
' Displayblanks_John_Doe Macro
'
' Keyboard Shortcut: Ctrl+Shift+J
'
Range("A3:L26").Select
Range("A3:L26,N3:N26").Select
Range("N3").Activate
Range("A3:L26,N3:N26,R3:R26").Select
Range("R3").Activate
Range("A3:L26,N3:N26,R3:R26,W3:W26").Select
Range("W3").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=LEN(TRIM(A3))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .Pattern = xlGray8
    .PatternColorIndex = xlAutomatic
    .ColorIndex = xlAutomatic
End With
Selection.FormatConditions(1).StopIfTrue = False
ActiveSheet.Range("$A$3:$W$26").AutoFilter Field:=7, Criteria1:= _
    "John Doe"
    Dim mycount As Long
    mycount = Application.WorksheetFunction.CountBlank(ActiveSheet.Range("$A$3:$W$26"))
    MsgBox mycount
End Sub

1 个答案:

答案 0 :(得分:0)

Range.SpecialCells methodxlCellTypeVisible property一起使用,以隔离AutoFilter method可见的单元格。

With ... End With statement语句可以帮助您引用工作簿中的特定区域,而无需诉诸.Select¹和.Activate¹。

Sub Displayblanks_John_Doe()
    ' Displayblanks_John_Doe Macro
    ' Keyboard Shortcut: Ctrl+Shift+J
    Dim mycount As Long

    With Worksheets("Sheet1")   'you should know what worksheet you are on
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A3:L26,N3:N26,R3:R26,W3:W26")
            .FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(TRIM(A3))=0"
            With .FormatConditions(.FormatConditions.Count)
                .Interior.Pattern = xlGray8
                .StopIfTrue = False
                .SetFirstPriority
            End With
        End With
        With .Range("A3:W26")
            .AutoFilter Field:=7, Criteria1:="John Doe"
            'there has to be visible cells because we haven't excluded the header row yet
            With Intersect(.SpecialCells(xlCellTypeVisible), _
                           .SpecialCells(xlCellTypeBlanks))
                mycount = .Cells.Count
                MsgBox mycount
            End With
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With

End Sub

上面的代码计算真正空白的可见单元格;不是可能是公式返回的零长度字符串的单元格(例如"")。

我意识到你的代码尚未完成;仅仅因为它实际上没有做任何事情,只报告了多个空白可见细胞。最初的步骤之一是找到G列中最后一个填充的单元格,并将其用作过滤器范围的范围。实现此目的的一种快速方法是在您引用工作表后立即引用Range.CurrentRegion property With ... End With statement; e.g。

With Worksheets("Sheet1")   'you should know what worksheet you are on
    With .Cells(1, 1).CurrentRegion
        'do all of your work here
    End With
End With

这可能不适合您的特定情况,因为您希望从第3行开始,但Range.Resize propertyRange.Offset property可以解决此问题。

With Worksheets("Sheet1")   'you should know what worksheet you are on
    With .Cells(1, 1).CurrentRegion
        With .Resize(.Rows.Count - 2, .Columns.Count).Offset(2, 0)
            'do all of your work here
        End With
    End With
End With

录制的宏代码是一个很好的起点,但您很快就会发现它非常冗长并经常记录不需要的动作。习惯在宏录制会话后清理代码,很快就会发现你是从头开始编写代码。

¹有关远离依赖选择和激活以实现目标的更多方法,请参阅How to avoid using Select in Excel VBA macros