3

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.

4

4 回答 4

4

也许是这样:

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

因此计算范围内以任何字符开头的所有单元格?

于 2014-04-24T10:20:30.997 回答
3

你真正想要的是:

FormulaResultCount = Evaluate("CountA(C:C)")

我刚刚了解了评估命令。这很棒!

它给了你 3 :)

于 2013-10-18T01:41:21.407 回答
3

xlCellType 公式。包含公式的单元格 -4123

这不会根据它们的值返回单元格,但它们是否有任何公式。根据您的工作表,您应该得到5

另外,请不要使用.Select 有趣的阅读

你的代码也可以写成

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

另一个提示:使用时SpecialCells,请使用适当的错误处理,这样如果没有符合SpecialCells条件的单元格,您的代码就不会中断。请参阅此示例。

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

评论的跟进

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
于 2013-10-18T01:33:30.477 回答
1

您可以在没有 VBA 的情况下仅使用公式来执行此操作。

=ROWS(range)*COLUMNS(range)-COUNTBLANK(range)

如果您尝试在 VBA 中执行此操作,可以使用以下命令:

Function non_blank_cell_results_count(r As Range) As Long
  non_blank_cell_results_count = r.Cells.Count - WorksheetFunction.CountBlank(r)
End Function
于 2014-05-01T18:38:38.540 回答