Nick's VBA Tip Of The Day
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
Attachment | Size |
---|---|
FOR_LoopPiglets.xls | 31.5 KB |
reset-used-range.xls | 30.5 KB |
ScreenUpdating-excel.xls | 32 KB |
time-VBA-code.xls | 31 KB |
FIND-VBA.xls | 36.5 KB |
»
- Nick's blog
- Login or register to post comments
- 37213 reads
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
you should use VLOOKUP for
you should use VLOOKUP for this
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:
http://excelexperts.com/33-vba-tips-output-array-without-looping
5. VBA Tip Of The Day - Time Your Code
How do you time how long it takes for VBA code to run ?
Simple..
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:
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 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:
Actually...
If you use VLOOKUP, INDEX, or MATCH in VBA, you can use it as
Application.vlookup etc it will not raise a runtime error.
Application.vlookup
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.