0

I need to count the number of blank cells, and cells that contain a number less than 60 in column O. Every time I try CountIf(Range("O:O"), "") I get a value of 1048539. I'm expecting way less than that. Am I going about this right? I can count the number of cells that are less than 60 perfectly fine.

The blank sells are set by an equation that makes them equal "" if they don't meet a condition.

I can now find out how many rows my data has, but I still cannot figure out how to count the number of empty cells.

Here is what I am trying.

Sub count()
   Dim blanks As Long
   Dim totalRows As Long
   totalRows = Application.WorksheetFunction.CountA(Range("O:O")) - 1
   For i = 1 To totalRows
        If (Application.WorksheetFunction.isBlank(Cells(i, "O"))) Then
            blanks = blanks + 1
        End If
   Next i
End Sub

I get the following error

Object doesn't support this property or method

I think it has to do with Cells() but I'm not sure what else I can use.

2 Answers2

0

In a rush right now, but something like this may help:

Sub CountCells()

   Dim TotalFilledRows As Long
   Dim TotalRows As Long
   Dim EmptyCells As Long
   Dim CellsWithNumbers As Long

   TotalFilledRows = Application.WorksheetFunction.CountA(Range("O:O"))
   TotalRows = Cells(Rows.count, 15).End(xlUp).Row

   For i = 1 To TotalRows
        If IsEmpty(Cells(i, 15)) Then
            EmptyCells = EmptyCells + 1
        End If

        If Application.WorksheetFunction.IsNumber(Cells(i, 15)) Then
            CellsWithNumbers = CellsWithNumbers + 1
        End If
   Next i

   MsgBox "Total: " & TotalRows & "; Filled: " & TotalFilledRows & "; Empty cells: " & EmptyCells & "; Cells with numbers: " & CellsWithNumbers
End Sub
CMArg
  • 1,525
  • 3
  • 13
  • 28
  • @pcw Yes, I know. I wanted to count total, empty and with numbers. Then make a calculation. I like your code, specially because is loopless. – CMArg Jun 25 '16 at 01:08
0
Dim blanks As Long
with range(range("o1"), range("o" & rows.count).end(xlup))
    blanks = application.countif(.cells,"") - .specialcells(xlcelltypeblanks).count
end with

Because:

countif(.cells,"") will return the number of "" and empty cells.

.specialcells(xlcelltypeblanks).count will return the number of empty cells.

PaichengWu
  • 2,649
  • 1
  • 14
  • 28
  • 1
    funny facts: `Application.CountBlank([range])` will count empty cells and cells with empty strings while `Application.CountIf([range], "=")` will only count cells which are emtpy... this way `Application.CountBlank(Range("O:O")) - Application.CountIf(Range("O:O"), "=")` is also an option ;) – Dirk Reichel Jun 25 '16 at 02:23
  • @DirkReichel, Thank you. I always feel confused about blank, empty and "" string. I do not know what Microsoft define **Blank**. `CountBlank()` returns th number of empty and "", but in VBA `Specialcells(xlCellTypeBlanks)` returns the number of empty only. – PaichengWu Jun 25 '16 at 04:19
  • Both Answers work, but I'm using this one because it's loopless –  Jun 27 '16 at 12:33