我是初学者,在Excel中使用VBA。我想提出一个看起来像this的用户表单。我有所有编码,但是当我从Excel中的命令按钮启动它时,ListBox不会填充。当我尝试输入数字并点击"提交"我得到"运行时错误''' ''当我单击debug时,它会转到
行
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

答案 0 :(得分:0)
可能有两个原因:
您的ListBox
MultiSelect
属性设置为1(fmMultiSelectMulti)或2(fmMultiSelectExtented)
在这种情况下,其Value
属性将始终为空
您的ListBox
未选择任何项目
即使其MultiSelect
属性设置为0(fmMultiSelectSingle),如果没有选择任何项,其Value
属性将返回Null
在这种情况下,使用ListIndex
属性设置支票,如下所示
If dotwListBox.ListIndex <> -1 Then Cells(emptyRow, 1).Value = dotwListBox.Value
由于-1是ListIndex
属性在未选择任何项目时返回的值