Professional Documents
Culture Documents
Analyze the logic: Before optimizing the syntax, pay more attention in optimizing the
logic. Without a good logic, a good written VBA macro program has no value. So
streamline your program logic and get the best performance of macros.
Hide Page Breaks:When we run a Microsoft VBA macro in a later version of Microsoft
Excel, the macro may take longer to complete than it does in earlier versions of
Excel. For example, a macro that required several seconds to complete in an earlier
version of Excel may require several minutes to complete in a later version of Excel.
This problem may occur if the following conditions are true:
Use 'WITH' statement when working with objects: If we have to access an object's
properties and methods in several lines, we must avoid using object's name or fully
qualified object path again and again. It is annoying for VBA processor as it needs to
fully qualify the object each time. (Isn't it annoying for us too when some work or
something is told us again and again? Got it Guys !
SLOW MACRO
FAST MACRO
With Sheets(1).Range("A1:E1")
.Font.Italic = True
Sheets(1).Range("A1:E1").Font.Italic = True
.Font.Interior.Color = vbRed
Sheets(1).Range("A1:E1").Font.Interior.Color = vbRed
.MergeCells = True
Sheets(1).Range("A1:E1").MergeCells = True
End With
The point here to understand is minimum qualifying of an object by VBA processor.
i.e. using minimum dots/periods(.) in the code. This concept tells us to use [A1]
rather than Range("A1") and Range("StockRange")(3,4) rather than
Range("StockRange").Cells(3,4)
Use vbNullString instead of ""(2 double quotes) : vbNullString is slightly faster than
"", since vbNullString is not actually a string, but a constant set to 0 bytes, whereas ""
is a string consuming at least 4-6 bytes for just existence.
For example: Instead of strVariable = "", use strVariable = vbNullString.
Reduce the number of Lines: Avoid multiple statements especially when they can be
clubbed into one line. For example - See these 2 macros
SLOW MACRO FAST MACRO
With Selection
.WrapText = True With Selection
.ShrinkToFit = False .WrapText = True: .ShrinkToFit = False
End With End With
As you can see, you can club multiple statements into one using colon character(:).
When you do this with multiple statements, it will decrease the readability but will
increase the speed.
Compiler Logic: When we save the macro, it is virtually compiled and unlike it's
human readable form as we saw in VBE(Visual Basic Editor), keywords(the dark blue
words which you cannot use as variable) are saved as three-byte token which are
faster to process as machine understand them better and variables, comments and
literal strings which are not either keyword or directive are saved "as is". However
VBA compiler tokenizes the word but it does not compress the lines and each line is
maintained as is ending with the 'Carriage Return'. When the VBA macro is executed,
VBA processor fetched single line at a time. The tokens of the fetched line saved by
virtual compiler are now interpreted and executed then next line is fetched and so
on. When we combine multiple lines using colon into one line, we're reducing the
number of fetch cycles the processor must go through.
Our Suggestion: This change will bring minor difference in time due to faster
processors today. Moreover, you cannot have more than 255 characters in a line and
you won't be able to debug your code using F8 efficiently. So it's a kind of useless,
there is no reason to trade-off with readability with such a minor change in time.
Declare variable as Variable and constant as Constant: Seems, obvious ! But many of
us don't follow it. Like
Dim Pi As Double
Pi = 3.14159
instead use
Const Pi As Double
Pi = 3.14159
Since, its value is never changed so it will be evaluated once during compilation
unlike variable which are evaluated many times during the run-time.
Use 'For Each' rather than 'indexed For': We can avoid using Indexed For when
looping through collections. For example, take the code just before this tip. It can be
modified to:
For Each myCell in Range("C5:C10")
mProduct = mProduct * myCell.Value
Next
This is in relation to qualifying object again and again as using "WITH" statements.
Avoid using 'Macro Recorder' style code:Ya, the code will look genius and eventually
perform like Genius too ! You'll better catch it with example, so use:
[A1].Interior.Color = vbRed
rather than
Range("A1").Select
Selection.Interior.Color = vbRed
Using too many Select and Selection effects the performance drastically. Ask yourself
why to go in Cell and then change the properties? or rather ask why to go pizza shop
when you can enjoy it at your home ;)
Avoid using Variant and Object in declaration statements: Think about better logic
and get rid of them. i.e. do not use Dim i As Variant or Dim mCell As Object. By trying
to be specific,we will save a lot of system memory this way, particularly in case of
large projects. We may not remember which has been declared variant above and
misuse a variable assigning any value to it which will be type-casted without errors. A
variant's descriptor is 16 bytes long while double is 8 bytes, long is 4 bytes and integer
is 2 bytes. Hence use Variant cautiously. As an example, use:
Dim i As Long rather than Dim i As Variant
Similarly use:
Dim mCell As Range 'or
Dim mSheet As Worksheet
rather than
Dim mCell As Object 'or
Dim mSheet As Object
Declare OLE objects directly: Declaring and defining the OLE objects in declaration
statement is called 'Early Binding' but declaring an object and defining it later is
called 'Late Binding'. Always prefer 'Early Binding' over 'Late Binding'. Now for example
use:
Dim oXL As Excel.Application
rather than
Dim oXL As Object
Set oXL = CreateObject("Excel.Application")