Share via

What does the phrase Compile error: "Invalid Outside procedure"

Anonymous
2018-09-04T15:03:53+00:00

I keep getting this message, Compile error: "Invalid Outside procedure" . I have no experience in coding, therefore could someone please explain this in layman's term.

Thanks,

JeanMarie

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2018-09-04T15:30:45+00:00

    You have copy some lines of code into the editor without a "hull" => a Sub or Function

    In other words, there are some lines not inside a sub / function.

    Andreas.

    Was this answer helpful?

    10+ people found this answer helpful.
    0 comments No comments
  2. HansV 462.6K Reputation points
    2018-09-04T15:28:03+00:00

    A code module can consist of several sections.

    At the top, you can have instructions that specify how the Visual Basic interpreter works. For example, the line

    Option Explicit

    means that you have to declare all variables that you use; referring to an undeclared variable will cause an error.

    Next, you can declare variables and constants (and some other things) that can be used throughout the code module (or even in all modules in the same workbook). For example:

    Const MyName="JeanMarie"

    Dim StartDate As Date

    Everything below that must be procedures (starting with Sub and ending with End Sub) and functions (starting with Function and ending with End Function). Lines of code that are not within either a procedure or a function will cause the error that you experience.

    So for example this is OK:

    Option Explicit

    Const MyName="JeanMarie"

    Dim StartDate As Date

    Sub Test()

        MsgBox "Hello World"

    End Sub

    Function Triple(x As Long) As Long

        Triple = 3 * x

    End Sub

    But the following will cause an error because there is a line in between the procedure and the function:

    Option Explicit

    Const MyName="JeanMarie"

    Dim StartDate As Date

    Sub Test()

        MsgBox "Hello World"

    End Sub

    StartDate = Now ' This causes an error

    Function Triple(x As Long) As Long

        Triple = 3 * x

    End Sub

    Was this answer helpful?

    10+ people found this answer helpful.
    0 comments No comments