【发布时间】:2020-08-26 09:17:15
【问题描述】:
我有一个用户表单,我希望用户在其中选择两个列表框中的每个单词,然后将它们保存在工作表中。用户也可以(理论上)留下预选的单词。这是我写的代码:
Private Sub UserForm_Activate ()
With ListBox1 'This list is about the stake
.AddItem "Essential"
.AddItem "Important"
.AddItem "Not interesting"
End With
'then I try to initialize the value of the stake.
ListBox1.Value = "Important"
With ListBox2 'This is a second list, about priority
.AddItem "Auto"
.AddItem "Yes"
.AddItem "No"
End With
'then I try to initialize the value of the priority
Listbox2.Value="Yes"
End Sub ()
但我的问题是,即使这两个列表似乎已正确初始化(运行用户窗体时列表中突出显示的正确单词),我也无法提取其中一个列表的值。当我运行以下代码时:
Private Sub CommandButton2_Click()
Feuil1.Cells(1,1)=Listbox1.Value
Feuil1.Cells(1,2)=Listbox2.Value
End sub ()
Excel 能够提取 Listbox2(优先级)的值:“是”,但不能提取 Listbox1(股权)的值:“重要”。 我不明白为什么该代码适用于其中一个而不适用于另一个!
还有一个元素:如果我在列表中手动选择一个单词,那么 Excel 能够为我提供两个列表框的值。
有什么线索吗?
【问题讨论】:
-
我无法重现您的问题。命令按钮将单元格 a1 和 b1 的值设置为用户窗体列表框中的选定值
-
您无法重现该问题并不让我感到惊讶,因为它似乎没有意义。而今天 Excel 无法提取的值发生了变化,现在是无法提取的 listbox2(优先级)。不知道为什么这不起作用并破坏了我的整个(和更复杂的)代码/项目,这非常令人沮丧!感谢您的尝试!
标签: excel vba listbox userform