Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting non blank cell results without looping in Excel VBA -- e.g., with Specialcells

Tags:

dynamic

excel

vba

Here is the code I'm trying to count with in VBA, hoping to return a count return variable of "3" from 'FormulaResultCount'. Why can't I count what is visibly returned by the formulas within each cell; from the grey box (see photo below)?

 Sub countNonBlanks()

        Worksheets("Sheet1").Select
        Range("C:C").Select


        FormulaResultCount = Selection.SpecialCells(xlCellTypeFormulas).Count


            'SpecialCells options from MSFT            
            '        xlCellTypeAllFormatConditions. Cells of any format -4172
            '        xlCellTypeAllValidation. Cells having validation criteria -4174
            '        xlCellTypeBlanks. Empty cells 4
            '        xlCellTypeComments. Cells containing notes -4144
            '        xlCellTypeConstants. Cells containing constants 2
            '        xlCellTypeFormulas. Cells containing formulas -4123
            '        xlCellTypeLastCell. The last cell in the used range 11
            '        xlCellTypeSameFormatConditions. Cells having the same format -4173
            '        xlCellTypeSameValidation. Cells having the same validation -4175
            '        xlCellTypeVisible. All visible cells
            '

    End Sub

See formula as reference:

enter image description here

Note: Since I will have many more cells when working dynamically, loops will likely slow the process down too much. Also, I tried using CountA without result.

like image 605
Bob Hopez Avatar asked Nov 18 '25 06:11

Bob Hopez


2 Answers

Maybe this:

FormulaResultCount = WorksheetFunction.CountIf(Range("C:C"), "?*")

Thus counting all cells in range that start with any character?

like image 120
user3568343 Avatar answered Nov 21 '25 01:11

user3568343


xlCellTypeFormulas. Cells containing formulas -4123

This would not return the cell based on their values but if they have any formula or not. As per your worksheet, you should get 5

Also, PLEASE PLEASE do not use .Select INTERESTING READ

Your code can also be written as

FormulaResultCount = Worksheets("Sheet1").Columns(3).SpecialCells(xlCellTypeFormulas).Count

Another Tip: When using SpecialCells, use appropriate error handling so that if there are no cells which match the SpecialCells criteria, your code won't break. See this example.

Sub Sample()
    Dim ws As Worksheet
    Dim Rng As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        On Error Resume Next
        Set Rng = .Columns(3).SpecialCells(xlCellTypeFormulas)
        If Err <> 0 Then
            MsgBox "No Cells with formulas were found"
            Exit Sub
        End If
        On Error GoTo 0
    End With

    FormulaResultCount = Rng.Count

    Debug.Print FormulaResultCount
End Sub

FOLLOWUP From Comments

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        Debug.Print Evaluate("=COUNTA(C1:C" & lRow & _
                    ")-COUNTIF(C1:C" & lRow & ","""")")
    End With
End Sub
like image 44
Siddharth Rout Avatar answered Nov 21 '25 00:11

Siddharth Rout