我有一本Excel工作簿,其中有两个同时使用的用户窗体。有各种按钮,文本框,更改标签等,一旦设置,只需单击第二个用户窗体,然后单击第一个,即可将其重置。它曾经可以在没有这种意外重置的情况下工作,并且程序中没有任何更改,所以我不知道为什么会发生这种情况。它包含与工作相关的内容,因此我无法发布实际的代码,但是我希望有人以前对此有所经验。任何帮助将不胜感激。
这是您管理表单的方式。这里有一个小片段,将为您提供基础知识。它要求有一个名称为MyForm
的用户窗体,其中带有一个名为TextBox1
的文本框,以及一个单击时会隐藏该窗体的命令按钮。
Sub CallUserForm()
Dim Frm As MyForm
' This command creates a new instances of the form.
Set Frm = New MyForm
With Frm
' you now have full access of the form
.TextBox1.Value = "Good morning!"
' but it isn't shown until you show it.
.Show
' now Frm has taken control
' and will retain control until it meets the Me.Hide command
' after "Hide" code sumes here
MsgBox .TextBox1.Value
End With
' although hidden, the form still exists in memory.
' you could show it again, unchanged
If MsgBox("Do you want to show the form again?", vbYesNo) = vbYes Then
Frm.Show
Else
Unload Frm ' now the form is destroyed
' other instances may still exist
End If
Set Frm = Nothing
End Sub
[更多的基础教学将使您使用Show
命令开始表格,并以Unload Me
结束演出。我上面的代码的目的是向您显示一个对象(窗体),并且如果您从未将其分配给变量,则您永远不会获得对其的控制权。在我上面的代码中,您可以访问Show
之前和之后的表格。
而且,当然,一旦您Unload Me
,表格及其所含的所有内容都将消失。如果仅在单击命令按钮时用Hide
用Me.Hide
表单,您就不会期望发生这种情况。