Compile error expected end of statement vba

Compile error: Expected: end of statement

Introduction

Every programming language has its own grammar and vocabulary — technically known as syntax. Having a mastery of any language implies that you have a sound knowledge of its syntax.

As a beginner in VBA, you might be confronted with frustrating errors like the “Expected: end of statement” error. Be rest assured, no matter how experienced you’re with VBA coding, errors are always going to be a part of it.

The difference between a novice and an expert VBA programmer is that the expert programmers know how to effectively handle and use errors. This article will help you better understand the error above.

Types of errors in VBA

There are three types of errors in VBA: syntax errors, compile errors, and runtime errors.

Syntax Error

A syntax error, as you can guess from the name, occurs when VBA finds something wrong with the syntax in your code. When you type a line of code, VBA checks if the syntax is correct.

As soon as you hit enter, if VBA finds that something is missing in the syntax, it instantly shows a message with some text that can help you understand the missing part, as you can see in the screenshot below:

Compile error: Expected: Then or GoTo

Note: You need to enable theAuto Syntax Check’ in the VBA option for the error dialog box to appear when there is an error. If not, VBA will only highlight the line without showing the error dialog box. The gif below shows you how to enable the error dialog box in VBA.

Compile Error

Compile errors occur when something is missing that is needed for the code to run. For example, in the code below, as soon as you run the code, it will show an error.

Compile error: Block if without End if

Note the difference between the syntax error and the compile error. The syntax error occurs even before you run the code and the font of the problematic line turns to red. The compile error occurs when you try to run the code and VBA identifies that something is missing.

Run Time Errors

Runtime errors are those that occur when the code is running. Run time errors will occur only when all the syntax and compile errors have been taken care of. They are not due to errors in the code itself, but rather due to factors external to the code — like a wrong input by the user, or an object that is not existing.

For example, if you run code that is supposed to activate an Excel worksheet, but that worksheet is unavailable (either deleted or its name changed), your code would give you a runtime error.

Unlike with the two previous errors, when a runtime error occurs, the message in the Run-time error dialog box is a little more explicit because it explains the problem and that can help you correct it.

Coming back to our specific error (“Expected: end of statement”), let’s write and run some code that will generate the error.

Step 1: Open the Visual Basic Editor and create a new module as seen in the gif below.

Step 2: Write or copy and paste the following code:

Sub GenerateError() Dim i As Integer = 5 End Sub

Before you even run the code, you will have the following result:

Compile error: Expected: end of statement

The error comes from the fact that two statements have been written in one line instead of two. The code should be:

Line 1: Dim i As Integer

Possible reasons for the “Expected: end of statement” error

From the types of errors in VBA described above, you must have guessed that the “Expected: end of statement” error is a syntax error. As such, the possible reasons for the error are as varied as the number of mistakes that you can make while writing a line of code.

Without being exhaustive, below is a list of possible reasons for that error:

1) Writing two statements in one line (see the example above)

How to fix: Check to see if two different statements have inadvertently been put on the same line then send the second statement to a new line.

2) Absence of parentheses

How to fix: Make sure you have parentheses (both open and close) where necessary.

3) Absence of white space

Compile error: Expected: end of statement

How to fix: Any identifier that is immediately followed by a & , like name& and affiliation& , is interpreted as a Long variable, so the lack of whitespace in front of the concatenation operator (&) is causing a parse error. To solve the problem, you just need to put a space between the variables and the concatenation operator. Instead of writing name& , write name & .

About the author Joseph Maabo

J. Maabo is a health economist currently working as a consultant. He's had a passion for programming since his early years. Joseph has taught computerized accounting and statistics for many years, developing a robust knowledge of Excel and VBA programming. He is bilingual (French and English) and enjoys sharing his passion for programming with others.