Consolidating finding non-blank range into 1 line

时间:2016-07-11 22:59:12

标签: vba excel-vba excel

I am trying to count the number of non-blank cells in a column B. I know the syntax on this is off but I am not sure how to make it work correctly. I am trying to avoid doing a loop and and make it as simple as possible. Any thoughts would be greatly appreciated.

With Dashboard
            Total_Emails = Dashboard.Cells(Dashboard.Rows.Count, "B").End(xlUp).Row - WorksheetFunction.CountBlank(Range("B1:B&Dashboard.Cells(Dashboard.Rows.Count, "B").End(xlUp).Row))

        End With

2 个答案:

答案 0 :(得分:2)

Use CountA to count all the non-blanks in a range.

Total_Emails = WorksheetFunction.CountA(Columns("B"))

答案 1 :(得分:0)

I figured that it is actually easier to split it up into several different expressions. This works correctly.

    Dim CountLast As Long
        CountLast = Dashboard.Cells(Dashboard.Rows.Count, "B").End(xlUp).Row
    Dim Answer As Long
        Answer = WorksheetFunction.CountA(Range("B1:B" & CountLast))

        With Dashboard
            Total_Emails = CountLast - Answer
        End With