0

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

4

3 回答 3

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
于 2013-06-03T04:07:01.900 回答
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 = "" 
于 2013-06-03T02:33:48.920 回答
-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
于 2013-06-03T02:35:15.053 回答