如何停止在VBA循环

我创build了一个循环VBA来根据列A,B,C,D中的数据更新值。 但是一旦“D”列为空或者没有值,我就需要停止这个循环。

Sub Macro1() ' ' Set Do loop to stop when an empty cell is reached. Do Until IsEmpty(ActiveCell) Range("C2").Select Range("C2").End(xlDown).Offset(1, -2).Select Selection.ClearContents Range("C2").End(xlDown).Offset(0, -2).Select Range(Selection, Selection.End(xlUp)).Select Selection.FillDown Range("C2").End(xlDown).Offset(1, 0).Select ActiveCell.FormulaR1C1 = "Logged out" ActiveCell.Offset(1, 0).Select Loop End Sub 

有很多方法可以完成这个任务。 这是一种方法。

 Sub Test1() 'UpdatebyExtendoffice20161222 Dim x As Integer Application.ScreenUpdating = False ' Set numrows = number of rows of data. NumRows = Range("D1", Range("D1").End(xlDown)).Rows.Count ' Select cell D1. Range("D1").Select ' Establish "For" loop to loop "numrows" number of times. For x = 1 To NumRows ' Insert your code here. ' Selects cell down 1 row from active cell. ActiveCell.Offset(1, 0).Select Next Application.ScreenUpdating = True End Sub 
 If isEmpty(Range(x)) then exit do end if