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