2015-04-23 82 views
0

說我有5列,上帝知道有多少行。除了最後一列中的幾個單元格外,此範圍內的所有單元格都將填充。 我需要遍歷所有行(從A1開始),直到我到達最後一行,並且對於在最後一列中填充了單元格的每行,顯示一個消息框,指出「你好」在excel中循環遍歷未知範圍的行

I'我真的不確定如何開始循環。 我試過Google搜索,但我不明白。我知道如何檢查空單元格,以及如何浸入消息框,但不知道如何查找範圍的結尾。

+0

http://stackoverflow.com/questions/6301665/how-to-count-the-number-的-行式-Excel的與數據 –

回答

0

從我身邊的其他變種

Dim cl As Range 
With ActiveSheet 
    For Each cl In .Range(.[A1].CurrentRegion.Columns(5).Address) 
     If cl.Value <> "" Then 
      MsgBox cl.Address(0, 0) & " has a value (" & cl.Value & ")" 
     End If 
    Next cl 
End With 
0

只要沒有完全空白的列或行,.currentregion屬性應該爲您提供對完整的矩形單元塊的引用。

dim rw as long 
with activesheet.cells(1, 1).currentregion 
    for rw = 1 to .rows.count 
     if not isempty(.cells(rw, 5)) then 
      msgbox .cells(rw, 5).address(0, 0) & " has a value (" & .cells(rw, 5).value & ")" 
     end if 
    next rw 
end with 
0

很多版本的

Sub Button1_Click() 
    Dim Rws As Long, Rng As Range, c As Range, x 
    Rws = Cells(Rows.Count, "A").End(xlUp).Row 
    Set Rng = Range(Cells(1, 1), Cells(Rws, 1)) 

    For Each c In Rng.Cells 
     x = WorksheetFunction.CountA(Range(Cells(c.Row, 1), Cells(c.Row, 5))) 
     If x = 5 Then MsgBox "Hello! Row #- " & c.Row & " has 5!" 
    Next c 

End Sub