2. Learn VBA - FOR Loops

Nick's picture

Loops are fundamental to programming, and in this tip, we'll look at FOR loops.

If you have never programmed before then this step by step guide will show you how to write your first program.

Q. What is a loop ?

A. I think this is best explained using an example.

Suppose you're at a farm, and a pig has just given birth to 10 piglets. The farmer asks you which is the biggest piglet. In order to answer this question, you will look at how big each piglet is, and once you have looked at all the piglets, you'll make your decision. A loop is the programming equivalent of looking at each piglet. Now lets use this example.


Step1: When you have the data as shown in the picture, Press ALT + F11 to bring up the VBA editor. Step2: You'll now need to insert a module, so right click on the blue area shown in the screen shot, and go: insert=>module


Step3: Now, in the white space provided, type the following code:


Step4: Click anywhere within the code, and press F5 - F5 runs code. You will now see a message

box showing which piglet is the biggest.


Download workbook to play with


VBA training Video - FOR Loops