[英]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.