Runtime Error 3: “Return without GoSub,” explained and fixed

Error 3, “Return without GoSub”, means code reached a Return statement without arriving there through a matching GoSub. Execution fell into the subroutine block by accident, usually by running past the end of the main code, and the Return had nowhere to go back to. This is always a bug in the code's structure, never a problem with the computer or the document.

For people who just run the program

  1. Note the macro or button you ran. Error 3 appears the moment the code takes a wrong turn, so what you clicked is the main clue.
  2. Do not try a different document first. The same macro fails the same way in every file, because the wrong turn is built into the code itself.
  3. Restart the application and run it once more only to confirm it repeats. A repeating error 3 is certain, not a glitch.
  4. Send the maintainer the error number and the exact steps. The fix is a small edit in the macro, and the click path tells them which block of code is being fallen into.

For people who write the code

  1. End the main flow before the subroutine block. The classic cause is a labeled block sitting directly after the main code with no Exit Sub or Exit Function in between:
  2. Sub Report()
        GoSub CalcTotals
        MsgBox "Done"
        Exit Sub        ' without this line, flow falls into the block
    CalcTotals:
        total = total * 2
        Return
    End Sub
  3. Replace GoSub with a real procedure. Pass values as arguments and return results with a Function. GoSub blocks share all local variables with the caller, which is exactly how stray flow and mystery state get in.
  4. Check for a bare Return typed out of habit. Languages like C and Java use return to leave a procedure; in VBA, leaving is Exit Sub or Exit Function, and Return is only legal inside a GoSub block.
Error 3 never means the file is corrupted. It is the interpreter catching a control-flow mistake the moment it happens. If a macro started showing error 3 after an edit, the edit moved or added a block; compare against the previous version before changing anything else.

If it still happens

If the code visibly has an Exit Sub before every labeled block and error 3 still fires, look for a second entry point: an On Error handler that Resumes into the middle of the block, or a GoTo that lands between the label and the Return. Both produce the same error with correct-looking structure.

Common questions

Is error 3 the same in VB6 and VBA? Yes. The number, the message, and the cause are identical across classic Visual Basic and VBA in Office. A Return with no matching GoSub is invalid in both.

Can error handling cause error 3? Yes. An error handler that uses Resume to jump into a subroutine block, rather than back to the line that failed, enters the block without a GoSub and the Return at the end raises error 3.

Related errors

Runtime Error 20 · Runtime Error 35 · Runtime Error 5