Excel,for循环问题

时间:2013-06-03 02:26:44

标签: excel vba excel-vba

我正在尝试在Excel VBA中设置一个简单的for循环,它从指定的行开始,并迭代到文档的结尾?我似乎无法通过谷歌找到如何做到这一点。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

这将循环遍历行,直到找到空白单元格。

  Dim iRow As Integer
  iRow = 0
  Do
    iRow = iRow + 1
    'do something
    Debug.Print ActiveSheet.Range("A" & iRow).Value

  Loop Until ActiveSheet.Range("A" & iRow).Formula = "" 

答案 1 :(得分:1)

如果您希望用户指定启动哪个单元格,则可以使用。

Sub myLoop()
Dim userInput As String
Dim count As Integer
Dim first As Integer

userInput = InputBox("Which cell would you like to start in?", "Enter Range like 'A1'")
first = ActiveSheet.Range(userInput).Row
count = ActiveSheet.UsedRange.Rows.count

For i = first To count
    MsgBox "Row: " & i
    'Replace the above msgbox function with the code you would like to run
Next i
End Sub

答案 2 :(得分:-2)

Dim CurrentRow
Dim LastRow
Dim i

CurrentRow = ActiveCell.Row
CurrentColumn = ActiveCell.Column
LastRow = ActiveSheet.Rows.Count

For i = CurrentRow to LastRow
   Cells(i, CurrentColumn).Value = "X"
Next i