对于以下代码,它似乎可以工作,因为适当的值在列表框中突出显示。但是,当我尝试获取 ATT2 列表框的值时,它为空。当我获得 Att3 的列表框的值时,它就起作用了。我添加了一个手表,Att3 的 FormControl.Value 看起来很棒,ATT2 的值为空。两个列表框具有相同的属性。
奇怪的是。如果我转到 Att2 的列表框并将多选更改为多选,然后再更改回单选,则 Att3 的值也会变为“”,此时任何选择都不起作用。这似乎不符合逻辑。有什么想法吗?
Case "FLT8 - 4 Ft"
' Automatically select "32W" in the Lamp Wattage ListBox
If Me.Label_equipmentBaseline_Att2.Caption Like "*Wattage*" Then
For i = 0 To Me.ListBox_equipmentBaseline_Att2.ListCount - 1
If Me.ListBox_equipmentBaseline_Att2.List(i) = "32W" Then
Me.ListBox_equipmentBaseline_Att2.ListIndex = i
Exit For
End If
Next i
End If
' Automatically select "IS N" in the Ballast ListBox
If Me.Label_equipmentBaseline_Att3.Caption Like "*Ballast*" Then
For i = 0 To Me.ListBox_equipmentBaseline_Att3.ListCount - 1
If Me.ListBox_equipmentBaseline_Att3.List(i) = "IS N" Then
Me.ListBox_equipmentBaseline_Att3.ListIndex = i
Exit For
End If
Next i
End If
我尝试添加
Me.ListBox_equipmentBaseline_Att3.Value = Me.ListBox_equipmentBaseline_Att3.List(i)
之后Me.ListBox_equipmentBaseline_Att2.ListIndex = i
执行后,虽然
Me.ListBox_equipmentBaseline_Att2.ListIndex
= "32W",Me.ListBox_equipmentBaseline_Att3.Value
仍然是""。
不是答案,但评论太长了......
我会首先将通用代码分解为可重用的方法,然后从那里开始工作。
Sub Test()
SetListBox Me.ListBox1, "Val002" 'ignoring the return value in this case
Debug.Print Me.ListBox1.Value '> Val002
'you can use the return value if the match needed to succeed
If Not SetListBox(Me.ListBox1, "willNotMatch") Then
MsgBox "value not matched!", vbExclamation
End If
Debug.Print Me.ListBox1.Value '> Null
End Sub
'Set the list index for listbox `lo` to match `findValue`
' Optionally clear any existing selection if no match is found
Function SetListBox(lb As Object, findValue) As Boolean
Dim i As Long
For i = 0 To lb.ListCount - 1
If lb.List(i) = findValue Then
Debug.Print "Found '" & findValue & _
"' at listindex " & i & " in " & lb.Name
lb.ListIndex = i
SetListBox = True
Exit For
End If
Next i
If Not SetListBox Then Debug.Print _
"Value '" & findValue & "' not found in list " & lb.Name
'optional - deselect any existing selection if no match was made
If Not SetListBox Then lb.ListIndex = -1
End Function