VBA用户表一次又一次显示相同的用户表单

时间:2016-05-23 08:50:03

标签: excel vba userform

目前我正在编写一个excel宏。宏显示Userform。 在Userform中,用户可以选择一些内容。在用户选择了一些我称之为Userform.Hide以隐藏Userform并从表单中读取选择之后。选择完成后,我调用Unload Userform。现在,代码与选择进行交互。我想在循环中执行此操作,但是当代码尝试第二次显示Userform时。我得到一个例外,表格已经显示。我无法理解它,因为我调用了Unload Userform。当我在调试模式下执行时,everthing可以正常工作。

用户形式代码

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 Sub

ClassModule中的代码我在循环中调用它

   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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解您的问题,但这是我使用VBA调用表单的方式。假设您有取消确定按钮:

表格形式:

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