VBA计数如果为空/空并在顶行输出 - 循环列数组

时间:2018-05-31 15:28:44

标签: excel vba excel-vba

我想创建一个宏,它将在列范围内对非空白求和(我希望它在列的数组中循环)并在顶行显示输出(第12行)。

我使用了这篇文章Sum range-loop in VBA,但无法申请我的案例。

到目前为止,我已经这样做了:

Sub Count_blanks()
    Dim arrControlSheet As Variant
    Dim SumErrors As Integer
    Dim TtlErrors As Double
    Dim h As Long

    arrControlSheet = Array("D", "E", "F", "G", "H", "I", "J", "K", "L")

    For h = LBound(arrControlSheet) To UBound(arrControlSheet)
        With Sheets("Control")
            TtlErrors = Cells(12, arrControlSheet(h)) = Application.CountA(Range(Cells(13, arrControlSheet(h)), Cells(80, arrControlSheet(h))))
        .Range(arrControlSheet(h) & 12) = TtlErrors
        End With
    Next
End Sub

请参阅以下屏幕截图以获得更好的可视化效果:

Screenshot1

宏应该更新第12行,表示相应列中的非空单元格数(从第13行到第80行)。

非常感谢

1 个答案:

答案 0 :(得分:1)

Sub CalculateNonBlank()

Dim n As Integer

For i = 4 To 12

n = Application.WorksheetFunction.CountA(Range(Cells(14, i), Cells(Rows.Count, i))) Cells(12, i).Value = n

Next

End Sub