在excel vba中删除combobox中的空白,但保留索引

所以我从数据库中提取数据来填充combobox。 每个值作为与其关联的ID,我将该ID用作该值的combobox中的索引。 所以如果ID是2,我把它放在索引2中。第一个问题是,如果我不填充某些东西,我使用空格,它不会让我使用索引。 因此,如果我没有填充至less10个空格,那么我不能在索引10处使用ID为10的值。另一个问题是存在差距。 所以如果我把价值指标2,3,5,7,8,10有0,1,4,6,9的空白。 有没有办法摆脱空白,但保持索引? 任何和所有的帮助表示赞赏。 谢谢。

这是我如何把值放入combobox。 我正在使用DEPOT_ID作为索引。

ComboBox1.AddItem rs![DEPOT_NAME], rs![DEPOT_ID] 

您将需要使用临时结构来作为地图或词典。

在VBA中创build一个数组,然后在将项目分配给combobox时,使用combobox索引loggingtrue索引。 然后当它被选中时,你可以将索引从数组中取出。

自从我写vba以来已经很长时间了,所以这可能是可怕的。

例如。

 Dim ComboBoxIndexs(100) As Integer Dim ArrayIndex as Integer Dim SelectedIndex as Integer Dim SelectedDepotID as Integer 'for each combo box item (I can't remember loops in vba) 'allocating to each item ComboBox1.AddItem rs![DEPOT_NAME], rs![DEPOT_ID] ComboBoxIndexs(ArrayIndex) = rs![DEPOT_ID] ArrayIndex = ArrayIndex +1; 'end of for loop. 'When you select an item and want to read it. SelectedIndex = Combobox1.Value 'now get your depot id from array SelectedDepotID = ComboBoxIndexs(SelectedIndex)