Nick's VBA Tip Of The Day

Nick's picture

There definitely won't be VBA tips every day, but I think it's a good idea to separate the VBA tips from the Excel ones.

To Learn VBA, follow this link

FOR_LoopPiglets.xls31.5 KB
reset-used-range.xls30.5 KB
ScreenUpdating-excel.xls32 KB
time-VBA-code.xls31 KB
FIND-VBA.xls36.5 KB

extra column

I have a question about the FIND-VBA code.
How do add I an corresponding value to a new column?

For example next to every NAME the code adds an age looked up in a table.

Thanks Marcus

Nick's picture

you should use VLOOKUP for

you should use VLOOKUP for this

Nick's picture

6. VBA Tip Of The Day - ScreenUpdating

What can you do to make VBA code run faster? Well, when you run VBA code in Excel, you will notice that the screen flickerseach time an action is performed. Lets take a simple example:


If you run this, each time the cell below is selected, you will see that the screen has updated. This is fine as long as your macro is small, but if it is big, Screen Updating can take a long time, and turning it off can make VBA code run faster. Let’s prove that:


This code for me takes 0.64 seconds to run


Now, lets turn off Screen Updating and see the difference:



0.64 seconds vs 0.078 seconds The faster VBA code runs 8 times the speed of the slow code. If you run the same procedure many times a day, this can add up quickly, and allow you to go home earlier.

NOTE: I turned ScreenUpdating back on before displaying the messagebox... If you don't do that, you get something like this:


... which although it looks whacky is likely to annoy your users.

Download Spreadsheet to see how turning off Screen Updating speeds up VBA code


Training Video on how to make your code run faster by turning off ScreenUpdating:

Wow thanks...

I filled a sheet with data from an Oracle query (loop within a loop) - it took about 4 minutes to complete, it now takes 6 seconds!

speed up

for further speed improvements, take a look here:

Nick's picture

5. VBA Tip Of The Day - Time Your Code

How do you time how long it takes for VBA code to run ?


Step1: At the start of your code, create a variable called "Start" (or whatever)

Set it = Timer (that takes a snapshot of the time)


Step2: At the End of your code, you can use a messagebox to display how long it took.

MsgBox Timer - Start & " Seconds"


Download Spreadsheet to see example of how to Time VBA code

Training Video on how to time your code:

Nick's picture

4. VBA Tip Of The Day - Finding things using VBA

Don't use VLOOKUP, INDEX, or MATCH from VBA when finding things on a worksheet with VBA.

  • The problem with these is that when the function doesn't find something, it will return a VBA error which you then have to trap and handle successfully.
  • This is a very messy way of coding, and prone to errors.

The best way to look for things on a sheet is to use .Find

Here's an example that finds all whole instances of "Nick" on this sheet:



This code will find all instances of "Nick":


Download workbook with code on how to find all instances of a string on a worksheet

Training Video on how to use FIND in VBA:


If you use VLOOKUP, INDEX, or MATCH in VBA, you can use it as
Application.vlookup etc it will not raise a runtime error.

Nick's picture


Gr8 spot..

I had always used Application.worksheetfunction.vlookup which does return an error.

Thanks for the feedback

You're welcome..

Bear in mind that you'll lose the tab autocompletion of the function when you write the code.