0

I used conditional formatting to display blank cells and created macros by filtering by name of every individual who has blanks on the worksheet. Now I need to display the number of blank cells for each individual. How do I go about it .

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
Luuklag
  • 3,897
  • 11
  • 38
  • 57
  • Welcome to SO. Do you just want a list of individuals with blank cells and how many blanks they have? And by cells do you mean how many rows of data that are blank? Or many blank cells are in the entire column/row set for a given name? *(btw - I removed the `ActiveWindow.Scroll`'s from your code since they are, most likely, unnecessary outputs from the recorder)* – Scott Holtzman Feb 10 '16 at 21:45
  • here is a **array** formula that will work. I based it on your code ranges, but adjust as needed: `=COUNT(IF(G3:G26=Z3,IF(ISBLANK(A3:A26),1,0)))` - *be sure to **Ctrl + Shift + Enter** instead of just **Enter** when exiting cell edit mode. Then drag down your set of unique names listed in say, column Z. – Scott Holtzman Feb 10 '16 at 21:51
  • I want to know number of blank cells per each user (not rows) when they run a macro – sharon gorikapudi Feb 11 '16 at 14:49
  • then adjust the `A3:A26` range to be `A3:W26`. If you want it to be in VBA. You can wrap it in a Evaluate statement. – Scott Holtzman Feb 11 '16 at 15:05
  • Hi Scott , in the above code mycount = Application.WorksheetFunction.CountBlank(ActiveSheet.Range("$A$3:$W$26")) how do i put in the criteria selection say in this case John Doe is the criteria and I want to know how many blank cells he has – sharon gorikapudi Feb 15 '16 at 16:22
  • =COUNT(IF(G3:G26=Z3,IF(ISBLANK(A3:A26),1,0))) is showing as an error – sharon gorikapudi Feb 15 '16 at 16:50

1 Answers1

0

Use the Range.SpecialCells method with the xlCellTypeVisible property to isolate the cells left visible by the AutoFilter method.

The With ... End With statement statement can help you reference specific areas within your workbook without resorting to .Select¹ and .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

The above code counts visible cells that are truly blank; not cells that could be zero-length strings returned by formulas (e.g. "" ).

I realize that your code is not finished yet; simply because it doesn't actually do anything but report teh number of blank visible cells. One of the first steps would be to find the last populated cell in column G and use that as the extent of the filter range. A quick method of accomplishing this is to reference the Range.CurrentRegion property is a With ... End With statement as soon as you've referenced the worksheet; 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

This may not be appropriate for your particular situation as you wish to start at row 3 but the Range.Resize property and Range.Offset property can take care of this.

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

Recorded macro code is a good place to start but you will quickly find that it is very verbose and often records actions that are not desired. Get used to cleaning your code up after a macro recording session and you will soon find that you are writing the code from scratch.


¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Community
  • 1
  • 1
  • Hi when I run the above code it is showing as object doesnt support this property or method . I did include the Range.Resize property – sharon gorikapudi Feb 15 '16 at 17:20
  • If .AutoFilterMode Then .AutoFilterMode = False – sharon gorikapudi Feb 15 '16 at 17:40
  • Also I need to know only the number of blank cells after filtering the data – sharon gorikapudi Feb 15 '16 at 17:44
  • Don't know what I can offer. The [Worksheet.AutoFilterMode](https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.autofiltermode.aspx) property has been around since at least xl2003. What did you change? –  Feb 15 '16 at 17:50
  • I didnt change anything ..your code did work without the offset .. is still need to know the number of blank cells after applying filters against each individual person suppose "John Doe" , "Tom Harris" etc – sharon gorikapudi Feb 22 '16 at 17:11