有谁能解释一下为什么当for循环放在UserForm_Initialize
sub中时,我会失去数组。如果我将Unload me
移到PrintOut
下面,并用Me.Hide
替换它,数组就不会有问题。同时,即使在开始时有Unload Me
,rpvSheet
也会正确传递,如下所示。
''Declare variables
Dim recapArr() As Long, i As Long, j As Long, rpvSheet as Long
Private Sub CommandButton1_Click()
Unload Me
''Print preview
If CheckBox2 = True Then Worksheets(recapArr).PrintOut Copies:=TextBox1.Value, Preview:=True
If CheckBox3 = True Then Worksheets(rpvSheet).PrintOut Copies:=TextBox1.Value, Preview:=True
End Sub
Private Sub CommandButton2_Click()
Unload Me
''Direct print
If CheckBox2 = True Then Worksheets(recapArr).PrintOut Copies:=TextBox1.Value
If CheckBox3 = True Then Worksheets(rpvSheet).PrintOut Copies:=TextBox1.Value
End Sub
Private Sub UserForm_Initialize()
''Check for hidden sheets that will not print
j = 0
For i = 3 To 4
If Sheets(i).Visible = True Then
ReDim Preserve recapArr(j)
recapArr(j) = Sheets(i).Index
j = j + 1
End If
Next i
If Sheet5.Visible = True Then rpvSheet = 5
If Sheet6.Visible = True Then rpvSheet = 6
End Sub
1条答案
按热度按时间aij0ehis1#
当UserForm被卸载时,引用类型似乎会丢失。为什么要依赖正在关闭的对象中的变量?如果需要数组,请将
Unload Me
移动到打印作业之后。或者在模块中声明数组变量。Me.Hide
不会关闭UserForm。这就是数组持久存在的原因。