Excel VBA - 标签第一次是空白 运行 表单,之后总是落后 1 步
Excel VBA - Label is blank first time running the form, later always 1 step behind
我有一个带有 1 个标签 (lword) 的用户表单。每次代码 运行 时,标签都需要用变量更新。每次代码为 运行 时,activecell 都会更改,因此值也会更改。
第一次使用代码 运行 标签是空白的(我删除了属性中的默认标题)。应为标签提供值的变量正在更新并显示正确的值。我包含了一个 MsgBox 来测试这个(MsgBox word)。
当您第二次或以后每次 运行 表格时,它总是显示以前的值。意味着更新标签似乎总是落后一步。
这是我目前拥有的代码:
Sub Random_Words()
Dim i As Integer
Dim word As String
Dim answer As String
Dim Rng As Range
i = ActiveSheet.UsedRange.Rows.Count
Set Rng = Range("B1:B" & i)
Cells(Int((i * Rnd) + 1), 2).Select
word = ActiveCell.Value
answer = ActiveCell.Offset(0, -1).Value
MsgBox word
Load UserForm1
UserForm1.Show
UserForm1.lword.Caption = word
End Sub
我一直在尝试并阅读了几个网站,但没有任何结果。
Userform1.Repaint 也无济于事。
非常感谢,如果您需要更多信息,请告诉我。
如果将 ShowModal
属性 设置为 False
,您将看到预期的行为。
当行UserForm1.Show
执行时,而窗体是Modal,下一行不会执行,直到你自己关闭窗体(实际上它隐藏了窗体和允许子继续)。您永远不会在代码中执行 Unload
,因此表单会保留,并且标签的值会在 "closed" 之后设置为 word
的新值.下次您显示表单时(实际上不是重新加载),旧值会显示,等等。要验证我刚才描述的内容,请像这样修改您的代码:
. . .
MsgBox "word = " & word ' the new value is assigned to "word"
Load UserForm1
UserForm1.Show ' the old value of "word" is showing
UserForm1.lword.Caption = word ' the new value is assigned to the label
MsgBox "caption = " & UserForm1.lword.Caption
End Sub
我有一个带有 1 个标签 (lword) 的用户表单。每次代码 运行 时,标签都需要用变量更新。每次代码为 运行 时,activecell 都会更改,因此值也会更改。
第一次使用代码 运行 标签是空白的(我删除了属性中的默认标题)。应为标签提供值的变量正在更新并显示正确的值。我包含了一个 MsgBox 来测试这个(MsgBox word)。
当您第二次或以后每次 运行 表格时,它总是显示以前的值。意味着更新标签似乎总是落后一步。
这是我目前拥有的代码:
Sub Random_Words()
Dim i As Integer
Dim word As String
Dim answer As String
Dim Rng As Range
i = ActiveSheet.UsedRange.Rows.Count
Set Rng = Range("B1:B" & i)
Cells(Int((i * Rnd) + 1), 2).Select
word = ActiveCell.Value
answer = ActiveCell.Offset(0, -1).Value
MsgBox word
Load UserForm1
UserForm1.Show
UserForm1.lword.Caption = word
End Sub
我一直在尝试并阅读了几个网站,但没有任何结果。 Userform1.Repaint 也无济于事。
非常感谢,如果您需要更多信息,请告诉我。
如果将 ShowModal
属性 设置为 False
,您将看到预期的行为。
当行UserForm1.Show
执行时,而窗体是Modal,下一行不会执行,直到你自己关闭窗体(实际上它隐藏了窗体和允许子继续)。您永远不会在代码中执行 Unload
,因此表单会保留,并且标签的值会在 "closed" 之后设置为 word
的新值.下次您显示表单时(实际上不是重新加载),旧值会显示,等等。要验证我刚才描述的内容,请像这样修改您的代码:
. . .
MsgBox "word = " & word ' the new value is assigned to "word"
Load UserForm1
UserForm1.Show ' the old value of "word" is showing
UserForm1.lword.Caption = word ' the new value is assigned to the label
MsgBox "caption = " & UserForm1.lword.Caption
End Sub