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

AttachmentSize
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

Application.vlookup

So, just start typing it out fully and use the autocompletion then remove what you don't need. A bit fidgety, but until you're fully familiar with the syntax it works fine.

Nick's picture

3. VBA Tip Of The Day - Reset the Used Range


An annoying feature of Excel is not resetting the the used range. What is the used range ? - well, it's meant to be a square around all populated cells.

- How can you select the Used Range for this example ?

reset-used-range-VBA-1

Step1: Press CTRL + END - This will select the last populated cell

Step2: - Now press CTRL + SHIFT + HOME

reset-used-range-VBA-2

... all your data is now selected. Now, here's the annoying thing. Suppose we delete the last 4 rows, and press CTRL + END again. - the last cell that was populated (but isn't now) is selected.

reset-used-range-VBA-3 

To reset the used range, we need to resort to using VBA.

reset-used-range-VBA-4 

 

If this code is in the worksheet's macro module, and is run, pressing CTRL + END will now go to the correct cell.

Download workbook with code on how to reset the used range..

Check out this tip on how to view the worksheet's code if you are new to VBA

Training Video on how to reset the used range:

Is there a reason why this might not work- range reset

I found an instance where it did not reset and I just can't figure out why. What conditions might be exceptional? I finally cleared the entire page to see what would happen and then put in some new data. At that point when I deleted cells and ran the macro it did in fact reset to the new range. I did not change the macro from what it was before
thanks mark

Nick's picture

2. VBA Fundamentals: FOR loop

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.

Data

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

Data 

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

Data 

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.

Data 

Download workbook to play with

 

VBA training Video - FOR Loops

Nick's picture

1. Working with Worksheet Events


Using VBA, there is the functionality for Excel to track most of what a user is doing to a workbook.

Armed with this information, a system designer can make things happen that help make the user's life easier.

Today, we're going to program a simple example of tracking selection change events. i.e. when you select something different from what is currently selected, an event is triggered.

Step1: Open a blank workbook, and press ALT + F11 - this brings up the VBA editor.

Worksheet Events 

Now, double click on "Sheet1".. in blue in the picture.

- You are now in the code module of "Sheet1"

Step2: From the middle dropdown that says "General", select "Worksheet"

- VBA editor presents you with a subroutine that is designed to pick up the event that the selection has been changed.

Step3: In the empty line after "Private Sub", type the following: MsgBox "Hello" Press ALT + F11 again to return to sheet1, and press the DOWN ARROW to change the selection.

- You will now see the message box confirming that you have successfully trapped this event.

Here's an example sheet to play with

This functionality is used to great effect in my game of Noughts and Crosses, and in this Smart Table Sorting

 

Training Video - Worksheet Events