新建From1(窗体),新建Command1(按钮CommandButton),代码:
Option Explicit
Private Sub Command1_Click()
Call s_1
End Sub
Private Sub s_1()
On Error GoTo hErr
If 1 = 2 Then
Dim strA As String
Else
strA = "a"
Debug.Print strA
End If
Exit Sub
hErr:
Debug.Print Err.Number & " " & Err.Description
End Sub
运行结果:
a
如果注释掉Dim strA As String,那么程序就会出错。证明了,不管Dim在哪里,都会提前分配内存。