restart sub

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Greetings, below is a code that I am using on a userform and it works just fine. Basically it is being used to find equipment that needs inspection, and pass those inspection values onto a WB. The boss just asked me to add in an additional function where if the device does not pass inspection (so the "Fail" button gets clicked) then a second userform is generated to create a work order for repairs. That part is very straight forward and I have that in place (in this sub it is referred to as "CorrectiveActionForm").

The issue that I am having is that if the user inputs the wrong DeviceId, I get the messagebox telling them this, but the sub continues to run and it continues on and launches the corrective action form. What I would rather have it do is just go back to the beginning of the sub and start over (I think - lol) I have to leave the initial form open because I cycle it over and over as well as pass information from this form to the CA form, so I can't just close it and relaunch. And I don't want the user to have any other option on the messagebox other than "OK" so I think that I have to stick with that style of messagebox.

Is there a string of code that I can use (I guess right after the messagebox?) that will either stop the sub from running or restart it and go back to the beginning?

I am probably doing this completely wrong in the first place :/ I appreciate any input - thanks, Rick



Code:
Private Sub FailButton_click()
Dim Found As Range
 Dim i As Long
 Dim Lastrow As Long
 
'This string finds the device that you are inspecting on the WS, confirms that you have the correct one,
'that the bar code was read correctly, and enters in the inspection observations for that device.
    If Me.TextBox1.Value = "" Then
        MsgBox "Emergency Light ID was not read, rescan and try again. ", , "Rescan and try again"
    Else
        Set Found = Sheets("Emergency Lighting Log").Range("L:L").Find(What:=Me.TextBox1.Value, _
                                                       LookIn:=xlValues, _
                                                       LookAt:=xlWhole, _
                                                       SearchOrder:=xlByRows, _
                                                       SearchDirection:=xlNext, _
                                                       MatchCase:=False)
        If Found Is Nothing Then
            MsgBox "No match for " & Me.TextBox1.Value, , "No Match Found"
            
       
 [COLOR=#ff0000]     ' I think that this is where I want the sub to "restart".  [/COLOR] 


        Else
            Found.Offset(0, 7).Value = Me.TextBox2.Value
            Found.Offset(0, 3).Value = Now()
            Found.Offset(0, 8).Value = "FAIL"
            
       
            If CheckBox1.Value = True Then
            Found.Offset(0, 4) = "X"
            End If
            If CheckBox2.Value = True Then
            Found.Offset(0, 5) = "X"
            End If
            If CheckBox3.Value = True Then
            Found.Offset(0, 6) = "X"
            
         End If
        End If
    End If
    
    'CheckBox1.Value = False
    'CheckBox2.Value = False
    'CheckBox3.Value = False
    'TextBox1.Value = ""
    'TextBox2.Value = ""
       
 'This string gives me the remaining amount of devices left to be inspected.
    Me.TextBox4.Text = CStr(ThisWorkbook.Sheets("Emergency Lighting Log").Range("M1").Value)
    
 'This string gives me the device names of the remaining inspections.
    ListBox1.Clear
    Lastrow = Cells(Rows.Count, "L").End(xlUp).Row
    For i = 1 To Lastrow
        If Cells(i, 15).Value = "" Then ListBox1.AddItem Cells(i, 12).Value
    Next i
        
    TextBox1.SetFocus
    CorrectiveActionForm.Show
    
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
never mind - I figured it out, all i had to do is move the call for the second form up into the If/EndIf routine - duh
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top