0% found this document useful (0 votes)
63 views

Top Ten Tips To Speed Up Your VBA Code

The document provides ten tips for speeding up VBA code including turning off screen updating and calculations, disabling events, using the WITH statement, editing recorded macros, using vbNullString instead of empty strings, reducing lines using commas and colons, declaring variables with the smallest data type, and avoiding SELECT statements.

Uploaded by

Amit Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Top Ten Tips To Speed Up Your VBA Code

The document provides ten tips for speeding up VBA code including turning off screen updating and calculations, disabling events, using the WITH statement, editing recorded macros, using vbNullString instead of empty strings, reducing lines using commas and colons, declaring variables with the smallest data type, and avoiding SELECT statements.

Uploaded by

Amit Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

eident.co.uk

Top Ten Tips To Speed Up Your VBA


Code - Eident Training
updates

9–11 minutes

1. Turn off Screen Updating

This will stop the screen flickering and updating while executing or
running macros, and that will greatly speed up your code.

Sub Stop_ScreenUpdating ()

Application.ScreenUpdating = False

‘… (Your Code)

Application.ScreenUpdating = True

End Sub

Tip: Use Application.ScreenUpdating = False at the beginning of


your code and Application.ScreenUpdating = True before ending
of your code to control when to stop and start screen updates.

2. Turn off ‘Automatic Calculations’

This prevents calculations while executing or running macro, so


you can wait until a set of actions have been completed and
instruct calculations to update at the end once, rather than after
every update.

1 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

Sub Stop_Calculation()

Application.Calculation = xlCalculationManual

‘… (Your Code)

Application.Calculation = xlCalculationAutomatic

End Sub

Tip: Use Application. Calculation = xlCalculationManual at the


beginning of your code and Application. Calculation =
xlCalculationAutomatic before ending of your code.

3. Disable Events

Disabling Excel Events will help you to prevent or stop endless


loops while executing or running macros, especially if you have
worksheet or workbook events.

Sub Stop_Events()

Application.EnableEvents = False

‘… (Your Code)

Application.EnableEvents = True

End Sub

Tip: Use Application. EnableEvents = False at the beginning of


your code and Application. EnableEvents = True before ending of
your code.

4. Use ‘WITH’ Statement

Use the ‘WITH’ statement when working with Objects in macro. If


you are using several statements with same object, use ‘WITH’
rather than referencing them all individually.

without a with statement:

2 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

Sub Without_WITH()

Worksheets(“Sheet1”).Range(“A1”).Value = 100

Worksheets(“Sheet1”).Range(“A1”).Font.Bold = True

End Sub

using a with statement:

Sub Use_WITH()

With Worksheet(“Sheet1”).Range(“A1”)

.Value = 100

.Font.Bold = True

End With

End Sub

5. Edit Recorded Macros

While a recorded macro can provide useful insights into Excel’s


VBA syntax and references, it is always better avoid using all the
code from a recorded macro. It is likely to have a detrimental effect
on performance, so always review the macro and edit down the
code to ensure only essential executable lines remain.
Example: Change cell (“C2”) colour to yellow and font is bold.

Recorded Macro
If you record macro, the code could look like this:

Sub Macro1()

‘ Macro1 Recorded Macro

3 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

Range(“C2”).Select

Selection.Font.Bold = True

With Selection.Interior

.Pattern = xlSolid

.PatternColorIndex = xlAutomatic

.Color = 65535

.TintAndShade = 0

.PatternTintAndShade = 0

End With

End Sub

Written Macro:
The recorded macro can also be written like this:

Sub Change_Cell_Font()

With Range(“C2”)

.Font.Bold = True

.Interior.Color = 65535

End With

End Sub

6. Use vbNullString instead of “”

The ‘vbNullString’ is a Constant. It denotes a null String. It


occupies less memory than a zero length string (denoted by “”)
and is faster to process and to assign.

Label1.Caption = vbNullString

4 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

is slightly more efficient than

Label1.Caption = “”

7. Reduce the number of lines using comma (,) or colon (:)

There are some VBA statements which may be written in a single


executable line of code instead of multiple lines.

Example: We can declare variables on the same line, separating


each one with a comma:

Sub Declare_Variables()

Dim intFirstNumber As Integer, IntSecondNumber As Integer

End Sub

Instead of the following:

Sub Declare_Variables1()

Dim intFirstNumber As Integer

Dim IntSecondNumber As Integer

End Sub

Example: Use colon (:) to write multiple statements in a single line,


for example to assign values to variable:

Sub Use_Colon_ForMultipleLine()

Dim intFirstNumber As Integer, IntSecondNumber As Integer

intFirstNumber = 5: intSecondNumber = 10

End Sub

Instead of the following example:

Sub Use_Colon_ForMultipleLine1()

5 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

Dim intFirstNumber As Integer, IntSecondNumber As Integer

intFirstNumber = 5 intSecondNumber = 10

End Sub

8. Declare Variables with the smallest viable data type size

Always make sure you declare your variables and utilise the
smallest data type possible, especially for numbers.

Dim intRowCount as Integer

Check the data range for the capacity of values needing to be


held:

Data type Storage Range


size

Byte 1 byte 0 to 255

Boolean 2 bytes True or False

Integer 2 bytes -32,768 to 32,767

Long 4 bytes -2,147,483,648 to 2,147,483,647


(long
integer)

Single 4 bytes -3.402823E38 to -1.401298E-45 for negative


(single- values;
precision 1.401298E-45 to 3.402823E38 for positive
floating- values
point)

Double 8 bytes -1.79769313486231E308 to


(double- -4.94065645841247E-324 for negative
precision values;

6 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

floating- 4.94065645841247E-324 to
point) 1.79769313486232E308 for positive values

Currency 8 bytes -922,337,203,685,477.5808 to


(scaled 922,337,203,685,477.5807
integer)

Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335


with no decimal point;
+/-7.9228162514264337593543950335 with
28 places to the right of the decimal

Date 8 bytes January 1, 100 to December 31, 9999

Object 4 bytes Any Object reference

String 10 bytes 0 to approximately 2 billion


(variable- + string
length) length

String Length 1 to approximately 65,400


(fixed- of string
length)

Variant 16 bytes Any numeric value up to the range of a


(with Double
numbers)

Variant 22 bytes Same range as for variable-length String


(with + string
characters) length
(24
bytes on
64-bit

7 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

systems)

User- Number The range of each element is the same as


defined required the range of its data type.
(using by
Type) elements

Avoid Variants – It’s a simple thing but often overlooked. All


variables, parameters and functions should have a defined data
type. If the data is a string, then the data type should be defined as
string. If you don’t give a data type, you’re using a variant. The
variant data type has its uses but not in string processing. A
variant means performance loss in most cases.

So add Option Explicit statements to each module and Dim all


variables with a decent data type. Review your functions and
ensure that they define a return data type.

The following functions are less than ideal if you’re using them on
strings as they apply to variants and they return variants. These
functions are OK to use if you’re processing variants, but wastefull
if working with strings.

Left(), Mid(), Right(), Chr(), ChrW(),

UCase(), LCase(), LTrim(), RTrim(), Trim(),

Space(), String(), Format(), Hex(), Oct(),

Str(), Error

If you’re dealing with strings of text, forget about the variants. Use
the string versions instead:

Left$(), Mid$(), Right$(), Chr$(), ChrW$(),

UCase$(), LCase$(), LTrim$(), RTrim$(), Trim$(),

8 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

Space$(), String$(), Format$(), Hex$(), Oct$(),

Str$(), Error$

9. Use the best approach to Copy and Paste

There are different approaches to copying data in VBA. The most


efficient is the direct copy action, missing out the Windows
Clipboard.
Example:

Sub CopyPaste_Direct()

Sheets(“Source”).Range(“A1:E10”).Copy
Destination:=Sheets(“Destination”).Range(“A1”)

End Sub

This example above is far more efficient than the “clipboard”


method below which sends the copied items to the clipboard to
then be pasted:

Sub CopyPaste_Clipboard()

Sheets(“Source”).Range(“A1:E10”).Copy

Sheets(“Destination”).Range(“A1”).PasteSpecial

Application.CutCopyMode = False

End Sub

10. Only add additional Reference Libraries when necessary

If you use objects in other applications as part of your Visual Basic


application, you may want to establish a reference to the object
libraries of those applications. Before you can do that, you should
check if that the application provides an object library.

To see if an application provides an object library

9 of 10 31-Jul-2023, 7:07 PM
Top Ten Tips To Speed Up Your VBA Code - Eident Training about:reader?url=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttps%2Feident.co.uk%2F2016%2F03%2Ft...

1. From the Tools menu, choose References to display the


References dialog box.

2. The References dialog box shows all object libraries registered


with the operating system. Scroll through the list for the application
whose object library you want to reference.
If the application isn’t listed, you can use the Browse button to
search for object libraries (*.olb and *.tlb) or executable files (*.exe
and *.dll on Windows).
References whose check boxes are checked are used by your
project; those that aren’t checked are not used, but can be added.

Consider Late Binding instead of Early Binding as an alternative.

See https://support.microsoft.com/en-gb/kb/245115

Check our calendar for scheduled VBA training courses in


Glasgow and Edinburgh or contact us to discuss your specific
training requirements in more detail

10 of 10 31-Jul-2023, 7:07 PM

You might also like