forked from farishadi/Excel_Macro_References
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVBA_OPTIMIZATION_README
More file actions
155 lines (115 loc) · 3.82 KB
/
VBA_OPTIMIZATION_README
File metadata and controls
155 lines (115 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Optimize Slow VBA Code. Speeding Up Slow Excel VBA Code (Sourced from http://www.ozgrid.com/VBA/SpeedingUpVBACode.htm)
Avoid the use of Copy and Paste whenever Possible:
Sub NoCopyAndPaste()
'Instead of:
Sheet1.Range("A1:A200").Copy
Sheet2.Range("B1").pasteSpecial
Application.CutCopyMode=False'Clear Clipboard
'Use:
'By-passes the Clipboard
Sheet1.Range("A1:A200").Copy Destination:=Sheet2.Range("B1")
'Or, if only values are needed:
Sheet2.Range("B1:B200").Value= Sheet1.Range("A1:A200").Value
'Or, if only formulas are needed:
Sheet2.Range("B1:B200").Formula = Sheet1.Range("A1:A200").Formula
'See also FormulaArray and FormulaR1C1 etc
'Instead of:
Sheet1.Range("A1:A200").Copy
Sheet1.Range("A1:A200").PasteSpecial xlPasteValues
Application.CutCopyMode=False'Clear Clipboard
'Use:
Sheet1.Range("A1:A200") = Sheet1.Range("A1:A200").Value
End Sub
Speed up code and stop screen flickering:
Sub NoScreenRePainting()
Application.ScreenUpdating=False
'Your code here.
Application.ScreenUpdating=True
End Sub
Preventing calculation while executing code:
Sub NoCalculations()
Application.Calculation = xlCalculationManual
'Your code here.
Application.Calculation = xlCalculationAutomatic
End Sub
Speeding up code if you have Worksheet or Workbook Events. Also stops endless loops in Events
Sub StopAllEvents()
Application.EnableEvents = False
'Your code here.
Application.EnableEvents = True
End Sub
Use the With Statement when working with Objects.
Sub WithARange()
With Range("A1")
.Value = 100
.Font.Bold = True
.Interior.ColorIndex = 6
.Copy Destination:=Range("B1")
End With
End Sub
Use VbNullString instead of = "" When needing to default a String variable back to it's default of "" use:
Sub EmptyText()
Dim strWords As String
strWords = "Cats"
MsgBox strWords
strWords = vbNullString
MsgBox strWords
End Sub
Inserting a Relative formula into a range of cells: Faster than AutoFill or Copy.
Sub NoAutoFillOrCopy()
Range("A1:A200").FormulaR1C1 = "=SUM(RC[1]:RC[5])"
End Sub
Tip: To get a formula, type it in any cell then select the cell, go Tools>Macro>Record new macro and record a macro pushing F2 then Enter.
Always declare your variables correctly!
To quickly view a variables definition:
Select the variable that you want the definition for.
Go to View>Definition (Shift+F2)
To return to your previous position:
Go to View>Last Postition (Ctrl+Shift+F2).
Release memory from Object variables:
Sub ReleaseObjectMemory()
'Could be any variable of the Object type
Dim wSheet as Worksheet
'Set Object variable
Set wSheet = Sheet1
'Your code here.
'Release memory
Set wSheet = Nothing
End Sub
Don't get caught in the Loop.
Follow this link (http://www.ozgrid.com/VBA/VBALoops.htm) to see why Loops should (and usually can) be avoided.
Avoid If, Else whenever possible
More often than not people would use an If, Else Statement to test whether a condition is TRUE or FALSE. There is however a slightly faster (and less typing) method. The first example shows the common method, while the second shows a faster method. Of course in such a small example the difference is not noticeable.
Sub TrueOrFalseSlower()
Dim bYesNo As Boolean
Dim i As Integer
If i = 5 Then
bYesNo = True
Else
bYesNo = False
End If
MsgBox bYesNo
End Sub
Here's a better way!
Sub TrueOrFalseFaster()
Dim bYesNo As Boolean
Dim i As Integer
bYesNo = (i = 5)
MsgBox bYesNo
End Sub
Another common need is to toggle a variable between True and False depending on a condition. For example:
Sub ToggleTrueOrFalseSlower()
Dim bYesNo As Boolean
If bYesNo = False Then
bYesNo = True
Else
bYesNo = False
End If
MsgBox bYesNo
End Sub
Here's a much better way
Sub ToggleTrueOrFalseFaster()
Dim bYesNo As Boolean
bYesNo = Not bYesNo
MsgBox bYesNo
End Sub