Top Ten Tips To Speed Up Your VBA Code
Top Ten Tips To Speed Up Your VBA Code
eident.co.uk
9–11 minutes
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
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
3. Disable Events
Sub Stop_Events()
Application.EnableEvents = False
‘… (Your Code)
Application.EnableEvents = True
End Sub
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
Sub Use_WITH()
With Worksheet(“Sheet1”).Range(“A1”)
.Value = 100
.Font.Bold = True
End With
End Sub
Recorded Macro
If you record macro, the code could look like this:
Sub Macro1()
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
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...
Label1.Caption = “”
Sub Declare_Variables()
End Sub
Sub Declare_Variables1()
End Sub
Sub Use_Colon_ForMultipleLine()
intFirstNumber = 5: intSecondNumber = 10
End Sub
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...
intFirstNumber = 5 intSecondNumber = 10
End Sub
Always make sure you declare your variables and utilise the
smallest data type possible, especially for numbers.
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
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)
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.
Str(), Error
If you’re dealing with strings of text, forget about the variants. Use
the string versions instead:
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...
Str$(), Error$
Sub CopyPaste_Direct()
Sheets(“Source”).Range(“A1:E10”).Copy
Destination:=Sheets(“Destination”).Range(“A1”)
End Sub
Sub CopyPaste_Clipboard()
Sheets(“Source”).Range(“A1:E10”).Copy
Sheets(“Destination”).Range(“A1”).PasteSpecial
Application.CutCopyMode = False
End Sub
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...
See https://support.microsoft.com/en-gb/kb/245115
10 of 10 31-Jul-2023, 7:07 PM