VBA Performance: Code 10x schneller machen
Lesezeit: 8 Minuten | Kategorie: VBA-Programmierung
Ihr VBA-Makro läuft ewig? 10.000 Zeilen in 30 Sekunden statt 0,5 Sekunden? Mit diesen 7 Optimierungen machen Sie Ihren Code bis zu 10x schneller – ohne die Logik zu ändern!
1. Application.ScreenUpdating = False
Das Problem: Excel zeichnet bei jeder Zelländerung den Bildschirm neu → Extrem langsam!
❌ Langsam (30 Sekunden):
For i = 1 To 10000
Cells(i, 1).Value = i
Next i
For i = 1 To 10000
Cells(i, 1).Value = i
Next i
✅ Schnell (0.5 Sekunden):
Application.ScreenUpdating = False
For i = 1 To 10000
Cells(i, 1).Value = i
Next i
Application.ScreenUpdating = True
Application.ScreenUpdating = False
For i = 1 To 10000
Cells(i, 1).Value = i
Next i
Application.ScreenUpdating = True
Wichtig: IMMER am Ende wieder auf True setzen – sonst bleibt der Bildschirm eingefroren!
2. Application.Calculation = xlCalculationManual
Bei jeder Zelländerung berechnet Excel ALLE Formeln neu → Bei 1000 Formeln extrem langsam!
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
' Ihr Code hier
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.ScreenUpdating = False
' Ihr Code hier
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
3. Arrays statt Ranges
Das größte Performance-Problem in VBA: Range-Zugriffe sind SEHR langsam!
❌ Langsam (15 Sekunden):
For i = 1 To 10000
Cells(i, 2).Value = Cells(i, 1).Value * 2
Next i
For i = 1 To 10000
Cells(i, 2).Value = Cells(i, 1).Value * 2
Next i
✅ Schnell (0.2 Sekunden):
Dim arr As Variant
arr = Range("A1:A10000").Value ' Array laden
Dim i As Long
For i = 1 To UBound(arr, 1)
arr(i, 1) = arr(i, 1) * 2
Next i
Range("B1:B10000").Value = arr ' Zurück schreiben
Dim arr As Variant
arr = Range("A1:A10000").Value ' Array laden
Dim i As Long
For i = 1 To UBound(arr, 1)
arr(i, 1) = arr(i, 1) * 2
Next i
Range("B1:B10000").Value = arr ' Zurück schreiben
Speedup: 75x schneller!
4. With-Statements nutzen
❌ Langsam:
Worksheets("Daten").Range("A1").Value = "Test"
Worksheets("Daten").Range("A1").Font.Bold = True
Worksheets("Daten").Range("A1").Font.Size = 14
Worksheets("Daten").Range("A1").Value = "Test"
Worksheets("Daten").Range("A1").Font.Bold = True
Worksheets("Daten").Range("A1").Font.Size = 14
✅ Schnell:
With Worksheets("Daten").Range("A1")
.Value = "Test"
.Font.Bold = True
.Font.Size = 14
End With
With Worksheets("Daten").Range("A1")
.Value = "Test"
.Font.Bold = True
.Font.Size = 14
End With
5. Select/Activate vermeiden
❌ Langsam:
Range("A1").Select
Selection.Value = "Test"
Range("A1").Select
Selection.Value = "Test"
✅ Schnell:
Range("A1").Value = "Test"
Range("A1").Value = "Test"
6. Dictionary statt Schleifen für Lookups
Problem: VLOOKUP/XVERWEIS in VBA mit Schleifen → O(n²) Komplexität!
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
' Lookup-Tabelle laden (1x)
For i = 1 To 1000
dict(Cells(i, 1).Value) = Cells(i, 2).Value
Next i
' Lookup (superschnell!)
For i = 1 To 10000
Cells(i, 3).Value = dict(Cells(i, 1).Value)
Next i
Set dict = CreateObject("Scripting.Dictionary")
' Lookup-Tabelle laden (1x)
For i = 1 To 1000
dict(Cells(i, 1).Value) = Cells(i, 2).Value
Next i
' Lookup (superschnell!)
For i = 1 To 10000
Cells(i, 3).Value = dict(Cells(i, 1).Value)
Next i
Speedup: 100x schneller als verschachtelte Schleifen!
7. Performance-Template
Sub PerformanceOptimiert()
Dim startTime As Double
startTime = Timer
' Performance-Booster aktivieren
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
On Error GoTo ErrorHandler
' === IHR CODE HIER ===
' Performance-Booster deaktivieren
Cleanup:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Debug.Print "Dauer: " & Format(Timer - startTime, "0.00") & " Sek"
Exit Sub
ErrorHandler:
MsgBox "Fehler: " & Err.Description
Resume Cleanup
End Sub
Dim startTime As Double
startTime = Timer
' Performance-Booster aktivieren
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
On Error GoTo ErrorHandler
' === IHR CODE HIER ===
' Performance-Booster deaktivieren
Cleanup:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Debug.Print "Dauer: " & Format(Timer - startTime, "0.00") & " Sek"
Exit Sub
ErrorHandler:
MsgBox "Fehler: " & Err.Description
Resume Cleanup
End Sub
Vor/Nachher Benchmarks
Test: 10.000 Zeilen mit Formel füllen
- ❌ Ohne Optimierung: 28,5 Sekunden
- ✅ Mit ScreenUpdating: 3,2 Sekunden
- ✅ Mit Arrays: 0,4 Sekunden
- ✅ Alle Optimierungen: 0,2 Sekunden
Speedup: 142x schneller! 🚀
Fazit
Diese 7 Techniken machen VBA-Code dramatisch schneller:
- ✅ ScreenUpdating = False
- ✅ Calculation = Manual
- ✅ Arrays statt Ranges
- ✅ With-Statements
- ✅ Select/Activate vermeiden
- ✅ Dictionary für Lookups
- ✅ Performance-Template nutzen