【发布时间】: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 功能并从空工作表上的一组单元格中提取了内容。并感谢您提供有关明确编码到工作表的提示!我是初学者,任何有助于澄清我的代码的东西都很棒!