Excel 允许从无模式表单开始,然后显示模态(但不能反过来)
我有一个包含 4 个用户表单的应用程序:UF1 - 选择合作伙伴,UF2 - 列出现有交易,UF21 - 显示现有交易,UF22 - 进行新交易。UF21 和 UF22 都源于 UF2。
UF21 需要无模式才能显示多个交易并并排比较,因此 UF1、UF2 和 UF21 都是无模式的。但我希望 UF22 成为 Modal 以便一次发出一笔新交易。
我的问题是,在我关闭 UF22 之后,即使只是从表格中转义,所有以前的表格都会关闭。我应该能够回到UF2。如果我使 UF22 无模式一切正常。
我编写了一个函数来遍历 UserForms 集合,并且能够获得对要激活的表单对象的引用。因此,我能够(在调试模式下)返回到 UF2 这是一个列表框,激活列表框,但在最后一个挂起的语句之后 UF2 和 UF1 都关闭。
由于模态和无模式形式的性质,我试图做的事情是不可能的,还是我应该继续推动正确的代码?
由于我最初的问题仍然悬而未决,并且我对@PeterT 提议的解决方案的测试实现无法正常工作,因此我根据@PeterT 的建议包含了我目前拥有的代码。
'===============
' Form UF1
'===============
Private Sub UserForm_Activate()
If ActivateUF22(FormID) = True Then Exit Sub
'.... more commands
End Sub
'============
' Form UF2
'============
Private Sub UserForm_Activate()
If ActivateUF22(FormID) = True Then Exit Sub
'.... more commands
End Sub
'----------------
Private Sub Cbn_OpenUF22_Click()
If ActivateUF22() = True Then
Exit Sub
Else
With New UF22
.Show vbModeless
End With
End If
End Sub
'================
' In a Module...
'================
Public Function ActivateUF22() As Boolean
Dim frm As Object
Set frm = GetFormFromID("UF22*") ' Custom function to get a form Object based on
' some criterion (FormID in a hidden TextBox)
If Not frm Is Nothing Then
' the only way I know to *Activate* an already .Show(n) form and compensate
' for the fact that the Close CommandButton may already have Focus
frm.TBx_UF22_CODE.SetFocus
frm.CBn_UF22_CLOSE.SetFocus
ActivateUF22 = True
Else
ActivateUF22 = False
End If
End Function