Chandrasekar R
Chandrasekar R

Reputation: 123

How to stop Loop in Vba

I have created a Looping VBA to update values according to data in Columns A,B,C,D. But I need to stop this Looping once 'D' Column is empty or has no values.

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

Upvotes: 0

Views: 1150

Answers (2)

ASH
ASH

Reputation: 20362

There are many ways to accomplish this task. Here is one method.

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

Upvotes: 1

Anthony
Anthony

Reputation: 116

If isEmpty(Range(x)) then exit do end if

Upvotes: 0

Related Questions