麻烦停止我的循环

Do While Cells(i, 1).Value <> "" .... End If i = i + 1 Loop End Sub 

对。 它与数字正常工作,完美停止。 但是与文本。 它不停止。

理想情况下,我想停止在我的内容的最后一行,而不是我在Excel中的最后一行。 我设法使它与数字正常工作,但我不能用文本修复它。

任何帮助将是伟大的,因为我是VBA的初学者。

 Sub checkRoutine() Dim i As Integer Dim LastRow As Long i = 1 Do While Cells(i, 1).Value <> "" If IsNumeric(Cells(i, 1).Value) Then Cells(i, 2).Value = Cells(i, 1).Value & " " & Cells(7, 5).Value If Not IsNumeric(Cells(i, 1).Value) Then LastRow = Range("A" & Rows.Count).End(xlUp).row + 1 ActiveSheet.Cells(LastRow, "A").Value = Cells(i, 1).Value & " " & Cells(7, 5).Value End If i = i + 1 Loop End Sub 

正如许多人所build议的那样,您需要更改为使用For循环:

 Sub checkRoutine() Dim i As Long Dim LastRow As Long LastRow = Range("A" & Rows.Count).End(xlUp).row For i = 1 To LastRow If IsNumeric(Cells(i, 1).Value) Then Cells(i, 2).Value = Cells(i, 1).Value & " " & Cells(7, 5).Value Else LastRow = Range("A" & Rows.Count).End(xlUp).row + 1 Cells(LastRow, "A").Value = Cells(i, 1).Value & " " & Cells(7, 5).Value End If Next End Sub