【问题标题】:ListBox not populating列表框未填充
【发布时间】:2016-06-07 05:57:05
【问题描述】:

我是在 Excel 中使用 VBA 的初学者。我试图想出一个看起来像this 的用户表单。我有所有的编码,但是当我从 Excel 中的命令按钮启动它时,不会填充 ListBox。当我尝试输入数字并单击“提交”时,我得到“运行时错误'424':需要对象”。当我单击调试时,它会将我带到该行

Cells(emptyRow, 1).Value = dotwListBox.Value

我不确定发生了什么。任何帮助,将不胜感激!!这是我的代码:

Private Sub cancel_Click()
Unload Me
End Sub

Private Sub clear_Click()
Call UserForm1_Initialize

End Sub



Private Sub submit_Click()
Dim emptyRow As Long
'Make Sheet3 active
Sheet3.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
Cells(emptyRow, 1).Value = dotwListBox.Value
Cells(emptyRow, 2).Value = t235tocbTextBox.Value
Cells(emptyRow, 3).Value = t235codbTextBox.Value
Cells(emptyRow, 4).Value = apiphbTextBox.Value
Cells(emptyRow, 5).Value = apiturbiditybTextBox.Value
Cells(emptyRow, 6).Value = apitocbTextBox.Value
Cells(emptyRow, 7).Value = apicodbTextBox.Value
Cells(emptyRow, 8).Value = apibodbTextBox.Value
Cells(emptyRow, 9).Value = longbaydobTextBox.Value
Cells(emptyRow, 10).Value = asudobTextBox.Value
Cells(emptyRow, 11).Value = rasmlssbTextBox.Value
Cells(emptyRow, 12).Value = clarifierturbiditybTextBox.Value
Cells(emptyRow, 13).Value = clarifierphbTextBox.Value
Cells(emptyRow, 14).Value = clarifiernh3bTextBox.Value
Cells(emptyRow, 15).Value = clarifierno3bTextBox.Value
Cells(emptyRow, 16).Value = clarifierenterococcibTextBox.Value
Cells(emptyRow, 17).Value = clarifierphosphorusbTextBox.Value




End Sub

Private Sub UserForm1_Initialize()
'Empty t235tocbTextBox
t235tocb.Value = ""
'Empty t235codTextBox
t235codb.Value = ""


'Fill dotwListBox
With dotwListBox
.AddItem "Monday"
.AddItem "Tuesday"
.AddItem "Wednesday"
.AddItem "Thursday"
.AddItem "Friday"
End With

'Empty apiphbTextBox
aphiphb.Value = "1"
'Empty apiturbiditybTextBox
apiturbidityb.Value = ""
'Empty apitocbTextBox
apitocb.Value = ""
'Empty apicodbTextBox
apicodb.Value = ""
'Empty apibodbTextBox
apibodb.Value = ""
'Empty longbaydobTextBox
longbaydob.Value = ""
'Empty asudobTextBox
asudob.Value = ""
'Empty rasmlssbTextBox
rasmlssb.Value = ""
'Empty clarifierturbiditybTextBox
clarifierturbidityb.Value = ""
'Empty clarifierphbTextBox
clarifierphb.Value = ""
'Empty clarifiernh3bTextBox
clarifiernh3b.Value = ""
'Empty clarifierno3bTextBox
clarifierno3b.Value = ""
'Empty clarifierenterococcibTextBox
clarifierenterococcib.Value = ""
'Empty clarifierphosphorusTextBox
clarifierphosphorusb.Value = ""
End Sub

【问题讨论】:

  • dotwListBox 是空列表框吗?如果是这样,那么难怪这会给你一个错误。如果 ListBox 已正确命名,Sub UserForm_Initialize 应该可以工作。也许你想检查一下 ListBox 属性窗口中的(Name) 是否真的设置为dotwListBox?另外,我可以建议您通过将工作表和工作簿添加到 Cells(emptyRow, 1).Value 来明确开始编码。例如像这样With Sheet3 然后.Cells(emptyRow, 1).Value
  • 我使用的名称与属性窗口中的名称不同,所以我修复了它。它仍然没有填充 ListBox,所以我最终使用了 RowSource 功能并从空工作表上的一组单元格中提取了内容。并感谢您提供有关明确编码到工作表的提示!我是初学者,任何有助于澄清我的代码的东西都很棒!

标签: excel vba listbox


【解决方案1】:

可能有两个原因:

  1. 您的 ListBox MultiSelect 属性设置为 1 (fmMultiSelectMulti) 或 2 (fmMultiSelectExtented)

    在这种情况下,它的 Value 属性将始终为 Null

  2. 您的ListBox 没有选择任何项目

    即使它的 MultiSelect 属性设置为 0 (fmMultiSelectSingle),如果没有选择任何项目,它的 Value 属性也会返回 Null

    在这种情况下,使用其ListIndex 属性设置检查,如下所示

If dotwListBox.ListIndex <> -1 Then Cells(emptyRow, 1).Value = dotwListBox.Value

因为 -1 是 ListIndex 属性在未选择任何项目时返回的值

【讨论】:

  • @srs1011:你通过了吗?
  • 是的,在某种程度上是这样。首先,我修正了 ListBox 的名称,因为我注意到我指的是不正确的名称。然后我最终不得不使用 RowSource 功能填充 ListBox,因为当我点击工作表上的命令按钮启动表单时,我无法让它填充。之后,一切正常!非常感谢!
  • 如果 ListBox 名称不正确,您将在 UserForm1_Initialize 子程序中遇到错误,该子程序在 submit_Click 子程序之前运行。在您的问题中,您说后者遇到了错误。此外,即使您成功填充了ListBox,然后尝试Cells(emptyRow, 1).Value = dotwListBox.Value,而之前没有让用户(或代码)选择一个值,您仍然会遇到错误。所以如果你还没有设置任何If dotwListBox.ListIndex &lt;&gt; -1 Then 检查,我认为你应该这样做。如果你能告诉我这两个问题,那就太好了。谢谢
  • 我还没有添加支票。到目前为止,在我的用户窗体中选择/填充的所有内容最终都会使用我现在运行的代码填充到工作表中。我想我只是不确定我原来的问题是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 2017-10-08
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多