Learn VBA : If Statements

Now that we understand how we can declare our variables we need to start to learn the basic VBA code that we will use in our surveys. By now you should know how to .ask() or .show() your questions , so this article takes you to the next level and shows you how to apply logic to your surveys so that your respondents only get to see the questions that are relevant to them. In this article we talk about IF statements.

When we talk about logic or routing there are probably only two statements that you will use. These are the IF and the Case statements. First of we will look at the IF. With an IF statement there are several ways we can write it but we recommend that you only use these two.

“If … Then … End If”
“If … Then … Else … End If”

Why only these two , well because they are the easiest to read when written and anything else can normally be done with the second command we will show you ( select case ). So back to the IF, let’s look at the “If … Then … End If”

When using this statement we need to follow this structure,

If ( Expression ) Then
	' Do something
End If

When you write your If statements , a good habit to get into is to write the skeleton code first and the fill in the logic and the do something later , that way you don’t forget to add any of the required keywords to the statement. And don’t forget to indent and make the code tidy. A sign of a Good programmer is neat code so take the time, if nothing else it makes it easier for your colleagues to read whilst you are on holiday. So first of we have the “If” keyword followed by an Condition in brackets, then the “Then” keyword followed what we want to do followed by the “End If” keywords. Some examples of “If … Then … End If” statements are

If ( Gender = {Male} ) Then
	Cars.ask()
	Beer.ask()
End If

or perhaps

If ( Gender = {Male} ) Then Cars.ask()

Notice in the second example that we don’t have to have the last “End If”, I think it is ok to do this if the If statement is short , like this one as it is easy to read on the screen without scrolling. Next let’s look at the “If … Then … Else” statement. The Skeleton for this is

If ( Condition ) Then
	' Do something
Else
	' Do something else
End If

As before we have our “If” keyword followed by the Condition and then the “Then” keyword and then the “‘Do something” if the Condition is true, but next rather than having the “End If” we have the “If … Then … Else” that allows us to do something else if the Condition is not true then Finally we have the “End If”. An example “If … Then … Else” statement is

If ( Gender = {Male} ) Then
	Cars.ask()
	Beer.ask()
Else
	Goto FemaleSection:
End If

The last thing we will talk about in this article is nesting statements. What we mean by this is having an if statement inside of another if statement. Here is an example of a nested If

Gender.ask()

If ( Gender = {Male} ) Then
	LikeMost.ask()
	If ( LikeMost = {Cars} ) Then
		Cars.ask()
	Else
		Beer.ask()
	End If
Else
	Goto FemaleSection:
End If

So as before we check to see if the respondent is male or female , if they are male we then ask a second question that checks what they like the most ( Cards or Beer ) and depending on their answer will depend on what question they get ask. If you do start to use nested If’s in your surveys it is always a good idea to , before you put any content in , just get the structure right , that way you won’t miss any If’s or End Ifs off from your syntax and make it difficult to debug. For example start with ,

If ( ... ) Then
	' Do something
Else
	' Do something
End If

then take a copy of this via the clipboard and paste it in again where you need it ,

If ( ... ) Then
	If ( ... ) Then
		' Do something
	Else
		' Do something
	End If
Else
	' Do something
End If

Then and the content in to get to your final statement.

Gender.ask()

If ( Gender = {Male} ) Then
	LikeMost.ask()
	If ( LikeMost = {Cars} ) Then
		Cars.ask()
	Else
		Beer.ask()
	End If
Else
	Goto FemaleSection:
End If

And there you have it some basic information about if statements. If you have any questions or need more examples then drop us a comment and we will be more than happy to help you out.

3 thoughts on “Learn VBA : If Statements”

Leave a Comment

%d bloggers like this: