Please note javascript is required for full website functionality.
MVP

Blog

If I Use VBA Then I Should Get Better at It!

24 March 2017

To see the start of our VBA blog series, click here

This week, we’re going to cover the use of the IF statement in VBA.  This is a very useful tool to help apply conditional rules in macros.  For example, if you want a macro to run A if a cell is positive, and B if it is negative, you can use the IF statement to check the cell value, and choose what will happen in the true and false cases.

The basic command looks like this:

If <X is true> Then

<Do something>

Else

<Do something else>

End If

You can also use it to run only if the check is true, as long as what you are doing will fit on one line, such as:

If <X is true> Then <Do something>

In this instance, if X is false, nothing will happen.

Let’s look at a specific example.  Suppose we want to check if the cell A1 contains a value.  If it does, let’s clear the content of the cell.  If not, we will copy the value from B1 into A1.  We could do this as follows:

If Range(“A1”).Value <> “” Then

Range(“A1”).ClearContents

Else

Range(“B1”).Copy

Range(“A1”).PasteSpecial xlPasteAll

End If

In this, the use of “<>” in the first line means “not equal to”; that is, that the value in cell A1 is not equal to a blank space.

 

Next Friday will be our Final Friday Fix, so stay tuned for that next week!  This blog will see you next in April!

Newsletter