我很难找到如何执行此操作的示例。我正在使用excel表来跟踪我的所有股票。我可以使用MyComboBox.List = MyTable.DataBodyRange.Value将整个表放在ActiveX组合框中,但我想过滤掉我没有库存的东西。当我尝试循环遍历表的行以过滤掉不需要的行时,我只获得组合框中表的最后一行。我尝试使用AddItem,但得到数据不匹配错误。我已经为此工作了2天。呃这是迄今为止的代码。
Private Sub UserForm_Initialize()
cmbStock.Clear
Dim sh As Worksheet
Dim cmb As ComboBox
Dim tbl As ListObject
Dim x As Long
Dim ttr As Long
Set cmb = frmAuctionBuilder.cmbStock
Set sh = ThisWorkbook.Sheets("Inventory2")
Set tbl = sh.ListObjects("TestInv")
'cmb.List = tbl.DataBodyRange.Value
ttr = tbl.ListRows.Count
'frmAuctionBuilder.cmbStock.AddItem (tbl.ListRows(6).Range.Cells(5))
'cmb.List = tbl.ListRows(9).Range.Value
'cmb.List = tbl.ListRows(10).Range.Value
For x = 1 To ttr
'Shows only last row of Table - combobox.List can not be added to I believe
'If tbl.ListRows(x).Range.Cells(5) > 0 Then cmb.List = tbl.ListRows(x).Range.Value
'Error on this
'If tbl.ListRows(x).Range.Cells(5) > 0 Then cmb.AddItem (tbl.ListRows(x).Range.Value)
Next x
End Sub