excel 用于打印工作表数组的用户窗体

x759pob2  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(139)

有谁能解释一下为什么当for循环放在UserForm_Initialize sub中时,我会失去数组。如果我将Unload me移到PrintOut下面,并用Me.Hide替换它,数组就不会有问题。同时,即使在开始时有Unload MerpvSheet也会正确传递,如下所示。

''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
aij0ehis

aij0ehis1#

当UserForm被卸载时,引用类型似乎会丢失。为什么要依赖正在关闭的对象中的变量?如果需要数组,请将Unload Me移动到打印作业之后。或者在模块中声明数组变量。Me.Hide不会关闭UserForm。这就是数组持久存在的原因。

相关问题