0

When there is an error, I want to go back to my userform and change the information entered on the form and re pass it into the macro and then continue from: If Len(Dir(sFilePath & newfol, vbDirectory)) = 0...

If Len(Dir(sFilePath & newfol, vbDirectory)) = 0 Then
            MkDir sFilePath & newfol & "\"
            On Error GoTo UserForm1
        Else
            MsgBox ("Folder already exists please re enter new folder name")
            'End
            UserForm1.Show
            MoveAndSave_Reports
End If

The above give me an error message: Compile error: Label not defined at "On Error GoTO UserForm1"

0

1 Answer 1

3

When you use an "On Error GoTo" statement, you're telling the program that when it hits an error, skip to a specific line in the current procedure. For example:

On Error GoTo ErrorHandler
x = 10 / 0
msgbox "x = infinity!"

ErrorHandler:
msgbox "Cannot divide by zero"

In this example, when the code hits an error (after my "On Error" statement), it will stop what it's doing and begin executing code at the ErrorHandler label (it's a label because of the colon at the end). Running this code, you would never see the message box saying x = infinity. Once it hits the error by trying to divide by zero, it will jump down to the ErrorHandler label and give me a message saying I can't divide by zero.

Chip Pearson has a good introduction to basic error handling here.

Sign up to request clarification or add additional context in comments.

1 Comment

As an add on to what Craig has said, make a label and then write some code to call the user form and do what needs to be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.