使用VBA中的值填充组合框

时间:2013-10-01 02:01:14

标签: excel vba combobox

我无法使用范围中的选项填充组合框。

用户选择带有refedit的范围,然后必须使用所选单元格的值填充ComboBox。如果用户更改了ref,则必须删除旧数据并使用新数据重新填充。

以下是我目前的代码。编译正确,但不起作用。

我本身并没有附加ComboBox,但是我需要使用列中的值填充列表,以便用户可以选择他们想要用作“键”的列表。第一组是连续的样本。我希望提供这些选项作为下拉列表的选项。

您可以在http://ge.tt/2dbV5Yt/v/0?c

下载我正在处理的内容的副本
Store #    Address City    ST  Zip Market  Radius
Private Sub rngHeader_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim selRng As Range
    Set selRng = Range(rngHeader.Value)

    '//Erase any items that are in there
    For I = 1 To cmbKeyCol.ListCount
        cmbKeyCol.RemoveItem 0 'Remove the top item each time
    Next I


    'Below here is the part that I'm having trouble with. This is one of my attempts, but
       'I've changed this thing probably 20 times since asking the question
    '//Build  new list of items from the header row.
    For Each cell In selRng.Cells
        cmbKeyCol.AddItem cell.Value
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

为什么不用这个来代替:

cmbkeyCol.RowSource = selRng.Address 'Assuming you already got your range right.

这会添加指定范围内的所有项目,而不是逐个迭代它们。

修改1:

Dim list as variant

list = selRng.Value
list = Application.Transpose(list)

cmbkeyCol.List = list

希望这有效。

编辑2:

Dim list as variant

list = rngHeader.value
list = Application.Transpose(list)

cmbkeyCol.List = list

我认为selRng是Edit 1中的源范围,实际上它是rngHeader。 希望现在有效。