我有一个在某个部分显示用户窗体的宏,我公开地将用户窗体中的一些变量变暗,以便在主宏中使用它。
现在我想在点击“继续!”后回到主宏。命令按钮。
这是我的主要宏:
Public C1 As String, C2 As String, C3 As String, C4 As String, C5 As String, C6 As String, C7 As String, C8 As String, C9 As String, C10 As String, C11 As String, C12 As String, C13 As String
Public NumB As Integer, NumofLines As Integer, Q1 As Integer, Q2 As Integer, Q3 As Integer, Q4 As Integer
Public Total As Double, Batch1Total As Double, Batch2Total As Double, Batch3Total As Double, Batch4Total As Double, Batch5Total As Double
Sub priority_calculation()
*Some code ....*
BatchDivider.Show
*Want to continue after hitting Proceed! commandbutton*
End Sub
主要有两种方法:
在您的表单代码中,添加一个事件
Public Event Proceed()
在您的继续按钮事件处理程序中,添加如下内容:
Private Sub btnProceed_Click()
RaiseEvent Proceed
End Sub
在您的调用代码中,这需要进行相当大的重组并且也在类模块中,将表单声明为对象with events:
Dim WithEvents BatchDivider As BatchDividerForm
单击继续时,事件处理程序将触发:
Private Sub BatchDivider_Proceed()
MsgBox "Proceed"
End Sub
这不会给你你想要的东西 - 在继续之后继续代码 - 但它会给你一种以结构化方式在代码的各个部分之间进行通信的方式。
这是更简单(也更丑陋)的方法。
在您的表单代码中添加:
Private m_Ready As Boolean
Private Sub btnProceed_Click()
m_Ready = True
End Sub
Property Let IsReady(IsReady As Boolean)
m_Ready = IsReady
End Property
Property Get IsReady() As Boolean
IsReady = m_Ready
End Property
在你的调用代码中,做这样的事情:
Dim BatchDivider As BatchDividerForm
Set BatchDivider = New BatchDividerForm
BatchDivider.IsReady = False
BatchDivider.Show
Do While Not FormInstance.IsReady
DoEvents
Loop
MsgBox "Let's proceed"
确保您的表单属性 ShowModal 设置为 False。
简而言之,这会为您的表单添加一个属性,该属性之前设置为 False。在表单显示后,它将测试该属性是否设置为 True。
调用表格:
Sub Tester()
Dim frm As New BatchDivider 'create an instance of the form
frm.Show 'show the form instance: code stop here until form is hidden/unloaded
Debug.Print frm.MyGlobal 'read the global from the (now hidden) form instance
Unload frm 'now done with the form....
End Sub
形式:
Public MyGlobal As String
Private Sub CommandButton1_Click()
MyGlobal = "value from form"
Me.Hide 'not unload
End Sub