VBA计算范围内的单元格数

时间:2014-06-09 09:48:14

标签: excel excel-vba vba

尝试计算状态栏上显示的范围内的单元格数。不确定如何计算范围内的单元格数量以用作进度条的分母。有什么想法吗?

For Each cell In Range("E11:G28,E33:G50,E57:G74,E79:G96,E101:G118,E130:G147,E152:G169,E175:G192,E198:G215,E221:G238")
    **lTotal = Range.Cells.Count**
    Application.StatusBar = "Processing cell " & cell.AddressLocal & _
                                    "    " & Format((lCounter / lTotal), "0%")
    cell.EntireRow.Hidden = (cell.Value = "")
    lCounter = lCounter = 1
Next cell

非常感谢

4 个答案:

答案 0 :(得分:2)

此版本逐行查看您的范围,而不是按单元格查看

它还会关闭ScreenUpdating以避免闪烁,重置StatusBar

Sub ReCut() 
Dim rData As Range 
Dim rng1 As Range 
Dim rng2 As Range 
Dim rcell As Range 
Dim lngCnt As Long 
Dim lngCalc As Long 

Set rData = Range("E11:G28,E33:G50,E57:G74,E79:G96,E101:G118,E130:G147,E152:G169,E175:G192,E198:G215,E221:G238") 

For Each rng1 In rData.Areas 
ltotal = ltotal + rData.Rows.Count 
Next 

With Application 
    .ScreenUpdating = False 
    .EnableEvents = False 
    lngCalc = .Calculation 
    .Calculation = xlCalculationManual 

    For Each rng2 In rData.Areas 
        For Each rcell In rng2.Rows 
            rcell.EntireRow.Hidden = (.CountBlank(rcell) = 3) 
            lngCnt = lngCnt + 1 
            .StatusBar = "Processing row " & lngCnt & "    " & Format((lngCnt / ltotal), "0%") 
        Next rcell 
    Next 


    .ScreenUpdating = True 
    .EnableEvents = True 
    .Calculation = lngCalc 
    .StatusBar = vbNullString 
End With 

End Sub  

答案 1 :(得分:1)

试试这个:

Sub Home()
    Dim cell As Range, N As Long
    For Each cell In Range("E11:G28,E33:G50,E57:G74,E79:G96,E101:G118,E130:G147,E152:G169,E175:G192,E198:G215,E221:G238").Areas
        N = N + cell.Cells.Count
    Next cell
    Debug.Print N
End Sub

这会计算您范围内的所有细胞。

答案 2 :(得分:1)

例如:

Dim rData As Range
Set rData = Range("E11:G28,E33:G50,E57:G74,E79:G96,E101:G118,E130:G147,E152:G169,E175:G192,E198:G215,E221:G238")
lTotal = rData.Count
For Each cell In rData.Cells
    Application.StatusBar = "Processing cell " & cell.AddressLocal & _
                                    "    " & Format((lCounter / lTotal), "0%")
    cell.EntireRow.Hidden = (cell.Value = "")
    lCounter = lCounter + 1
Next cell

答案 3 :(得分:1)

试试这个 -

Range(Selection.Address).Count