如何在多列表框中正确添加项目?
How do I properly add items in a multilistbox?
我正在使用 vba 获取设备编号及其相应信息并将它们放入列表框中。用户将输入他们想要的设备编号,然后 excel 将获取信息。但是,当我第一次单击我的 'Get Data' 按钮时,它工作正常。当我第二次为另一个设备编号执行此操作时,我收到消息 "Could not set the List property. Invalid property array index." 这是我的代码:
Dim value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
MWOList.AddItem (num)
MWOList.List(i, 1) = (num.Offset(0, 1))
MWOList.List(i, 2) = (num.Offset(0, 2))
MWOList.List(i, 3) = (num.Offset(0, 3))
MWOList.List(i, 4) = (num.Offset(0, 4))
MWOList.List(i, 5) = (num.Offset(0, 5))
i = i + 1
End If
Next num
i = i + 1
End Sub
试试下面,请注意,我不仅更改了 "i" 声明和值 public,而且列表列位置从 0 开始,所以如果这是 6 个元素 table,然后将其切换回来。
你出错的原因实际上是循环后的另一个"i"迭代"i=i+1",列表行也从0开始,因此你添加了第二个索引,并试图将它插入到第三名.
Public value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
i = MWOList.ListCount 'set i to available space
MWOList.AddItem
MWOList.List(i, 0) = (num.Offset(0, 1))
MWOList.List(i, 1) = (num.Offset(0, 2))
MWOList.List(i, 2) = (num.Offset(0, 3))
MWOList.List(i, 3) = (num.Offset(0, 4))
MWOList.List(i, 4) = (num.Offset(0, 5))
End If
Next num
EquipmentNumber = ""
End Sub
我正在使用 vba 获取设备编号及其相应信息并将它们放入列表框中。用户将输入他们想要的设备编号,然后 excel 将获取信息。但是,当我第一次单击我的 'Get Data' 按钮时,它工作正常。当我第二次为另一个设备编号执行此操作时,我收到消息 "Could not set the List property. Invalid property array index." 这是我的代码:
Dim value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
MWOList.AddItem (num)
MWOList.List(i, 1) = (num.Offset(0, 1))
MWOList.List(i, 2) = (num.Offset(0, 2))
MWOList.List(i, 3) = (num.Offset(0, 3))
MWOList.List(i, 4) = (num.Offset(0, 4))
MWOList.List(i, 5) = (num.Offset(0, 5))
i = i + 1
End If
Next num
i = i + 1
End Sub
试试下面,请注意,我不仅更改了 "i" 声明和值 public,而且列表列位置从 0 开始,所以如果这是 6 个元素 table,然后将其切换回来。
你出错的原因实际上是循环后的另一个"i"迭代"i=i+1",列表行也从0开始,因此你添加了第二个索引,并试图将它插入到第三名.
Public value As Long
Public i As Integer
Private Sub GetDataButton_Click()
Dim num As Variant
value = EquipmentNumber.value
For Each num In Sheets("S1 Cvtg Eqt List").Range(Range("B1"), Range("B1").End(xlDown))
If num = value Then
i = MWOList.ListCount 'set i to available space
MWOList.AddItem
MWOList.List(i, 0) = (num.Offset(0, 1))
MWOList.List(i, 1) = (num.Offset(0, 2))
MWOList.List(i, 2) = (num.Offset(0, 3))
MWOList.List(i, 3) = (num.Offset(0, 4))
MWOList.List(i, 4) = (num.Offset(0, 5))
End If
Next num
EquipmentNumber = ""
End Sub