【发布时间】:2016-05-23 08:50:02
【问题描述】:
目前我正在编写一个 excel 宏。该宏显示一个用户窗体。 在用户窗体中,用户可以选择一些东西。在用户选择了一些东西后,我调用 Userform.Hide 来隐藏用户表单并从表单中读取选择。阅读选择后,我调用 Unload Userform。现在代码与选择进行交互。我想循环执行此操作,但是当代码第二次尝试显示用户窗体时。我收到表单已显示的异常。我无法理解,因为我调用了 Unload 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 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
有人可以帮我吗?
【问题讨论】:
-
我们需要查看您的代码,以便我们知道发生了什么。
-
我已将其添加到问题中
-
你写了"I call Unload Userform",意思是说你要卸载的userform其实就是以"Userform"命名的?
-
既然你说它在debug模式下可以完美运行,是不是代码跑得太快了?尝试在循环中再次调用用户表单之前插入
Sleep或Wait,仅用于测试目的。