如何通过VBA中的名称来引用多个组合框的名称

时间:2018-10-16 19:29:58

标签: excel vba combobox

我正在研究带有许多组合框的工作簿。 下面的代码是获取组合框列表并将其传递到单元格区域的代码。 有没有办法在VBA代码中用组合框的名称引用组合框? 我想通过包含变量而不是直接引用组合框的名称。例如,“ cboAct”和i.List = actList

代码如下;

Private Sub UserForm_Initialize()

Dim sheet As Worksheet
Dim actList() As String
Dim i As Integer


Set sheet = Worksheets("DB")


i = sheet.Range("B3").End(xlDown).row


ReDim actList(1 To i - 2)

k = 1

For Each c In sheet.Range("B3:B" & i)

    If k = 1 Then

        actList(k) = c.value

        k = k + 1

    ElseIf c.value <> actList(k - 1) Then

        actList(k) = c.value

        k = k + 1

    End If

Next c


cboAct1.List = actList
cboAct2.List = actList
cboAct3.List = actList
cboAct4.List = actList
cboAct5.List = actList
cboAct6.List = actList
cboAct7.List = actList
cboAct8.List = actList
cboAct9.List = actList
cboAct10.List = actList
cboAct11.List = actList
cboAct12.List = actList
cboAct13.List = actList
cboAct14.List = actList
cboAct15.List = actList

End Sub

我的问题是,我可以通过引用下面带有变量的组合框的名称来编写代码吗?我想通过引用特定组合框的名称而不是“在sheets.combo框中的每个组合”之类的代码来编写代码。

cboAct1.List = actList

cboAct2.List = actList

cboAct3.List = actList

......................

cboAct15.List = actList

1 个答案:

答案 0 :(得分:0)

我解决了问题!

For i = 1 To 15 Me.Controls("cboAct" & i).List = actList Next i