目前我正在编写一个excel宏。宏显示一个Userform。在Userform中,用户可以选择某些内容。在用户选择了一些内容之后,我称之为Userform.Hide,以隐藏Userform并从表单中读取所选内容。读取所选内容后,我调用卸载Userform。现在,代码与所选内容进行交互。我想在循环中这样做,但是当代码第二次尝试显示Userform时。我得到了一个例外,即表单已经显示。我无法理解它,因为我调用了卸载Userform。当我在调试模式下做这件事时,每件事都会像它应该做的那样工作。
用户型代码
Private Sub Image1_Click()
SelectCard 1
End Sub
Private Sub Image2_Click()
SelectCard 2
End Sub
Private Sub SelectCard(number As Integer)
SelectedNumber = number
Me.Hide
End Sub
Public Sub CardSelector_Activate(Cards As Cards)
Dim c As card
For Each Key In Cards.CardDictionary.Keys
Set c = Cards.CardDictionary.Items(Key - 1)
If c.value = 1 And c.played Then
Image1.Enabled = False
End If
If c.value = 2 And c.played Then
Image2.Enabled = False
End If
Next Key
number = SelectedNumber
CardSelector.Show
End SubClassModule中的代码--我在循环中称之为
Sub Costum(Spalte As Integer, Zeile As Integer, SpalteBeginn As Integer, Cards As Cards, CardsOpponent As Cards)
CardSelector.CardSelector_Activate Cards
Dim c As card
Dim number As Integer
number = CardSelector.SelectedNumber
Set c = Cards.CardDictionary.Items(CardSelector.SelectedNumber - 1)
SetCardAsPlaced c, Zeile, Spalte, SpalteBeginn
Unload CardSelector
End Sub有人能帮我吗?
发布于 2016-05-23 09:32:47
我不确定我是否完全理解您的问题,但这就是我如何使用VBA调用表单的方式。这是假设您有一个Cancel和OK按钮:
形式如下:
Option Explicit
Private m_ResultCode As VbMsgBoxResult
Private Sub btnCancel_Click()
Call CloseWithResult(vbCancel)
End Sub
Private Sub btnOK_Click()
' Store form control values to member variables here. Then ...
Call CloseWithResult(vbOK)
End Sub
Private Sub CloseWithResult(Value As VbMsgBoxResult)
m_ResultCode = Value
Me.Hide
End Sub
Public Function ShowMe(Optional bNewLayerOptions As Boolean = True) As VbMsgBoxResult
' Set Default to Cancel
m_ResultCode = vbCancel
' Execution will pause here until the form is Closed or Unloaded
Call Me.Show(vbModal)
' Return Result
ShowMe = m_ResultCode
End Function然后,调用它(请注意,frmLayers是我自己的VBA表单对象-您可以使用您的):
Dim dlgLayers As New frmLayers
If (dlgLayers.ShowMe(False) = vbOK) Then
' Proceeed
End If这对你的问题有帮助吗?如果我误解了,我很抱歉,如果需要的话,我会删除我的答案。
像xxxxx_Activate等都是框架调用的事件处理程序。例如,有一个用于激活的事件和一个用于初始化的事件。如果正确设置代码,通常不需要自己直接调用这些代码。见https://support.microsoft.com/en-us/kb/138819。
https://stackoverflow.com/questions/37386548
复制相似问题