简体   繁体   English

VBA用户窗体一次又一次显示相同的用户窗体

[英]VBA Userforms Show the Same Userform again and again

currently i am programming a excel macro. 目前,我正在编程一个Excel宏。 The macro shows a Userform. 宏显示一个用户窗体。 In the Userform the User can Select something. 在用户窗体中,用户可以选择某些内容。 After the User has selected something i call Userform.Hide to Hide the Userform and to read the Selection from the Form. 用户选择了某些内容后,我将调用Userform.Hide隐藏该用户窗体并从该窗体中读取选择内容。 After the selection was read i call Unload Userform. 读取选择后,我致电“卸载用户表单”。 Now the Code interacts with the selection. 现在,代码与所选内容进行交互。 I want to do this in a loop but when the Code trys to show the Userform the second time. 我想循环执行此操作,但是当代码尝试第二次显示用户窗体时。 I get a exception that the Form is already displayed. 我得到一个例外,该表单已经显示。 I cant understand it, because i called Unload Userform. 我无法理解,因为我致电了Unload Userform。 When i do it in debug mode everthing works as it should. 当我在调试模式下进行操作时,一切都会正常进行。

Userform Code 用户表单代码

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

Code in the ClassModule i call this in a loop 我在一个循环中调用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

Can someone help me here ? 有人可以帮我吗?

I am not sure if I fully understand your issue, but this is how I invoke a form using VBA. 我不确定我是否完全理解您的问题,但这就是我使用VBA调用表单的方式。 This is assuming you have a Cancel and OK button: 假设您有一个“ 取消”和“ 确定”按钮:

In the form: 形式:

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

Then, to call it (please note that frmLayers is my own VBA form object - you would use yours): 然后,调用它(请注意frmLayers是我自己的VBA表单对象-您将使用您的对象):

Dim dlgLayers As New frmLayers

If (dlgLayers.ShowMe(False) = vbOK) Then
  ' Proceeed
End If

Does this help you with your issue? 这对您的问题有帮助吗? I am sorry if I have misunderstood, and I will remove my answer if needed. 很抱歉,如果我误解了,我将在需要时删除答案。

Things like xxxxx_Activate etc. are event handlers called by the framework. 诸如xxxxx_Activate等之类的xxxxx_Activate是框架调用的事件处理程序。 So, for example, there is an event for activate and an event for initialize. 因此,例如,有一个用于激活的事件和一个用于初始化的事件。 You don't normally have to directly call these yourself if you set your code up correctly. 如果您正确设置了代码,通常不必自己直接调用这些函数。 See https://support.microsoft.com/en-us/kb/138819 . 请参阅https://support.microsoft.com/zh-cn/kb/138819

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM