2014-06-09 57 views
1

試圖計算一個範圍內的單元格數量以顯示在狀態欄上。不知道如何計算範圍內的單元格數量以用作進度欄的分母。有什麼想法嗎?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 

非常感謝

+0

爲什麼你要在多列上連續運行'cell.EntireRow.Hidden =(cell.Value =「」)'這條線?行可以取消隱藏然後再次隱藏。如果*任何*單元格是空白的,您是否想隱藏該行 - 如果是,則檢查是否有空白。 – brettdj

+0

@brettdj謝謝。我想只隱藏該行,如果該行中的所有單元格都是空白的。 例如如果E11是空白的,但是G11不是空白的,是否_not_隱藏該行。如果E11,F11和G11爲空,則隱藏該行。 如果你知道更快的方式來做到這一點,我會非常感激,因爲目前它非常緩慢。 – user3450844

+0

我已經更新了我的代碼,以逐行進行,而不是逐個單元格。 – brettdj

回答

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 
+0

非常感謝,但它並沒有隱藏行中所有單元格在該行中空白的行。它實際上並不隱藏任何行。 此外,lTotal = rData.Rows.Count返回18而不是180. 更新:這是因爲如果有公式,counta()認爲它不是空的。所有的單元格都有公式。 – user3450844

+0

但是你的範圍是每列只有列E:G?第二個問題是我的錯,因爲範圍有多個區域,我的回答只能通過第一個回答。將更新。 – brettdj

+0

正確答案是更換 'cell.EntireRow.Hidden =(.CountA(小區)= 0)' 與 'cell.EntireRow.Hidden =(Application.CountBlank(小區)= 5)' 只需要找出正確的行計數。 – user3450844

0

試試這個:

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 

此計算範圍內所有的細胞。

0

例如:

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 
0

試試這個 -

Range(Selection.Address).Count 
+0

用途:範圍(Selection.Address)。計數 – n3cao

+1

請編輯更多信息。僅限代碼和「嘗試這個」的答案是不鼓勵的,因爲它們不包含可搜索的內容,也不解釋爲什麼有人應該「嘗試這個」。我們在這裏努力成爲知識的資源。 –

相關問題