简体   繁体   English

计算非空白单元格结果而不在Excel VBA中循环-例如,使用Specialcells

[英]Counting non blank cell results without looping in Excel VBA — e.g., with Specialcells

Here is the code I'm trying to count with in VBA, hoping to return a count return variable of "3" from 'FormulaResultCount'. 这是我试图在VBA中使用的代码,希望从“ FormulaResultCount”返回计数返回变量“ 3”。 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: 参见公式作为参考:

在此处输入图片说明

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. 另外,我尝试使用CountA没有结果。

Maybe this: 也许这样:

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

Thus counting all cells in range that start with any character? 这样计算范围内以任何字符开头的所有单元格吗?

What you really might want is: 您真正想要的是:

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

I just learnt about the evaluate command. 我刚刚了解了评估命令。 It's awesome! 这很棒!

And it gives you 3 :) 它给你3 :)

xlCellTypeFormulas. xlCellTypeFormulas。 Cells containing formulas -4123 包含公式-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 根据您的工作表,您应该获得5

Also, PLEASE PLEASE do not use .Select INTERESTING READ 另外,请不要使用。选择.Select 阅读

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. 另一个提示 :使用SpecialCells ,请使用适当的错误处理,这样,如果没有符合SpecialCells条件的单元格,则代码不会中断。 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

You can do this without VBA, using only formulas. 您可以仅使用公式而无需VBA来执行此操作。

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

If you're trying to do this in VBA, you can use this: 如果您尝试在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有 VBA 的 Excel 中拆分字符串(单元格)(例如数组公式) - Split a string (cell) in Excel without VBA (e.g. for array formula) Excel VBA:将多个非唯一单元格内容(例如,给定名称)返回到在姓氏上搜索过的ComboBox - Excel VBA: Return mutiple non-unique cell contents (e.g. Given Name) to a ComboBox having searched on Surname 针对具有字符串数据(Excel、VBA、宏)的列循环和扫描例如 200 个字符串的列表 - Looping and scanning a list of e.g., 200 strings against a column with string data (Excel, VBA, Macros) 如何使用 VBA 在 Excel 的单元格中“评估公式” - 例如显示工作 - How to “Evaluate Formula” in cell in Excel using VBA - e.g. Show working VBA:将范围转换为单元格编号,例如 (1,1) - VBA: Convert range to cell numbers e.g. (1,1) Excel VBA 计算一行中的非空白单元格 - Excel VBA Counting Non-Blank Cells in a Row VBA:嵌入在单元格中的字符串(例如“35 英里”) - 如何根据距离有多大来格式化单元格? - VBA: String (e.g. "35 miles") embedded in cell - how to format cell based on how large the distance is? Apache POI-包含非英语字符(例如Thai和单元格为空)的列,然后跳过该单元格 - Apache POI - Column having non-english characters e.g. Thai and cell empty, then skips the cell 如何使用 VBA 将默认日期(例如 0/1/1900)替换为空白? - How to replace default date (e.g. 0/1/1900) with blank using VBA? Excel计算非空白单元格 - Excel Counting non blank cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM