This part of my code is looping; MsgBox "A New Service row will now be created" newRow =…
- User
This part of my code is looping; MsgBox "A New Service row will now be created" newRow = Cells(Rows.Count, "B").End(xlUp).Row + 1 Range("B" & newRow).Value = "Service" How do I stop this loop.
Private Sub Worksheet_Change(ByVal Target As Range) Dim rngG As Range Dim rngI As Range
Set rngI = Intersect(Target, Range("I6:I500")) If Not rngI Is Nothing Then If IsDate(rngI.Value) Then If rngI.Offset(0, -7).Value = "Service" Then MsgBox "Thank you for entering Service Date Completed. Please proceed to enter Service Schedule as number of days." rngI.Offset(0, -2).Select Exit Sub Else If rngI.Offset(0, -7).Value <> "Service" Then 'If Not doubleClickFlag Then MsgBox "Please proceed to change the Issue description (column B) as required." rngI.Offset(0, -7).Select 'End If End If End If End If End If
Dim rngD As Range Dim rngId As Range Dim rngB As Range Dim newRow As Long
Set rngG = ActiveCell
If Not IsNumeric(rngG.Value) Then MsgBox "Only numbers should be used to represent days before next service" Exit Sub End If Set rngId = rngG.Offset(0, 2) If Not rngId.Value = "" Then If Not IsDate(rngId.Value) Then MsgBox "Date Service Completed not present" Exit Sub End If End If Set rngB = rngG.Offset(0, -5) If Not rngB.Value = "" Then If rngB.Value <> "Service" Then MsgBox "Service Schedule Days must ONLY be entered for Services" Exit Sub End If End If
MsgBox "A New Service row will now be created" newRow = Cells(Rows.Count, "B").End(xlUp).Row + 1 Range("B" & newRow).Value = "Service" Range("C" & newRow).Value = rngId.Value Range("F" & newRow).Value = rngG.Offset(0, -1).Value Range("H" & newRow).Value = rngId.Value + rngG.Value Range("J" & newRow).Value = rngG.Offset(0, 3).Value Range("K" & newRow).Value = rngG.Offset(0, 5).Value Range("L" & newRow).Value = rngG.Offset(0, 5).Value
rngB.Value = "Serviced" rngG.Value = "" rngG.Offset(0, 1).Value = ""
Exit Sub
If Target.Column = 5 Then 'doubleClickFlag = False ColumnList Target End If End Sub
- Assistant
To stop the loop, you can use the "Exit Sub" statement after the line that sets the value of the new row:
MsgBox "A New Service row will now be created" newRow = Cells(Rows.Count, "B").End(xlUp).Row + 1 Range("B" & newRow).Value = "Service" Exit SubThis will immediately exit the subroutine and prevent any further code from executing.