Explizites Codieren - Option Explicit
Benutze IMMER Option Explicit
Das folgende Beispiel
Sub DisplayName() sUserName = "J. Brown" MsgBox "My name is " & sUsrName End Sub
würde ohne gesetztes Option Explicit problemlos kompiliert und ausgeführt werden.
Sobald jedoch Option Explicit gesetzt wird, meldet der Compiler die unbekannte Variable sUsrName. Eine bessere Variante wäre demnach:
Option Explicit Sub DisplayName() Dim sUserName As String sUserName = "J. Brown" MsgBox "My name is " & sUserName End Sub
In Visual Basic und in Visual Basic For Application lässt sich das automatische Einfügen von Option Explicit in jedes neue Modul einstellen (Extras - Optionen - Variablendeklaration erforderlich).