【问题标题】:how to refer name of several combo boxs by their name in VBA如何在 VBA 中通过名称引用几个组合框的名称
【发布时间】:2018-10-16 19:29:58
【问题描述】:

我正在编写一个包含许多组合框的工作簿。 下面的代码是一个获取组合框列表并将它们传递给单元格区域的代码。 有没有办法用组合框的名称引用 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

我的问题是,我可以通过引用下面带有变量的组合框的名称来编写代码吗?我想通过引用特定组合框的名称来编写代码,而不是像“For Each combo in sheet.combo box”这样的代码。

cboAct1.List = actList

cboAct2.List = actList

cboAct3.List = actList

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

cboAct15.List = actList

【问题讨论】:

  • 当你尝试这样做时会发生什么? ...你试过了吗?
  • 以上代码正常工作。我想通过包含变量而不是直接引用组合框的名称。例如,“cboAct” & i.list = actList
  • 我解决问题! For i = 1 To 15 Me.Controls("cboAct" & i).List = actList Next i

标签: excel vba combobox


【解决方案1】:

我解决了问题!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多