使用带有变量的Excel VBA命令

时间:2016-09-29 19:41:47

标签: vba excel-vba excel

我正在尝试重用一些代码,从而使调用中的控件成为一个函数来获得不同的结果。问题似乎是引用我想要更新的不同控件。

Function populate(num1, num2)

Dim index As Integer
index = ComboBox1.ListIndex

ComboBox2.Clear

Select Case index
    Case Is = 0
        With ComboBox2
            ComboBox2.List = Worksheets("Sheet1").Range("A1:A10").Value
        End With
    Case Is = 1
        With ComboBox2
            ComboBox2.List = Worksheets("Sheet2").Range("A1:A10").Value
        End With
    Case Is = 2
        With ComboBox2
            ComboBox2.List = Worksheets("Sheet3").Range("A1:A10").Value
        End With
End Select

End Function

Private Sub ComboBox1_Change()
Call populate(1, 2)
End Sub

Private Sub ComboBox3_Change()
Call populate(3, 4)
End Sub

Private Sub ComboBox5_Change()
Call populate(5, 6)
End Sub

当然,对于我调用的每个组合框(奇数),它应该更新第二个组合框的值(偶数)。但是,不是将代码复制到每个奇数编号的组合框,而是 d喜欢有一些实际上被函数调用重用的代码,如上所示。

不幸的是,我不确定如何在VBA中完成这项工作。毋庸置疑,我希望它会像(不起作用):

Function populate(num1, num2)

Dim index As Integer
index = ComboBox + num1 + .ListIndex

ComboBox + num1 + .Clear

Select Case index
    Case Is = 0
        With ComboBox + num2
            ComboBox + num2 + .List = Worksheets("Sheet1").Range("A1:A10").Value
        End With
    Case Is = 1
        With ComboBox + num2
            ComboBox + num2 + .List = Worksheets("Sheet2").Range("A1:A10").Value
        End With
    Case Is = 2
        With ComboBox + num2
            ComboBox + num2 + .List = Worksheets("Sheet3").Range("A1:A10").Value
        End With
End Select

End Function

还看了一下可能重复的问题,但是没有解决我的问题。事实上,即使主题看似重复,我也不认为问题是。我正在尝试组合部分命令和变量以创建我想要使用的命令,而可能的副本似乎想要稍微不同的东西......我可能是错的,但这对我来说似乎是这样。

3 个答案:

答案 0 :(得分:1)

您可以使用Sheet.Drawingobjects集合引用组合框和其他控件,如下所示:

ActiveSheet.DrawingObjects("ComboBox" & num2)

答案 1 :(得分:1)

假设您有用户形式的组合框,那么您的代码可缩短为:

Option Explicit

Sub populate(num1, num2)
    Dim shtName As String

    shtName = "Sheet" & (Me.Controls("Combobox" & num1).ListIndex + 1)
    With Me.Controls("Combobox" & num2)
        .Clear
        .List = Worksheets(shtName).Range("A1:A10").Value
    End With
End Sub

否则,如果你有ActiveX Excel组合框,那么它会稍微改为:

Option Explicit

Sub populate(num1, num2)
    Dim shtName As String

    shtName = "Sheet" & (ActiveSheet.DrawingObjects("ComboBox" & num1).Object.ListIndex + 1)
    With ActiveSheet.DrawingObjects("Combobox" & num2).Object
        .Clear
        .List = Worksheets(shtName).Range("A1:A10").Value
    End With
End Sub

答案 2 :(得分:1)

不是将数字传递给populate子例程,而是传递要处理的组合框:

Sub populate(cb1 As ComboBox, cb2 As ComboBox)
    Dim index As Integer
    index = cb1.ListIndex

    With cb2
        .Clear

        Select Case index
            Case 0
                .List = Worksheets("Sheet1").Range("A1:A10").Value
            Case 1
                .List = Worksheets("Sheet2").Range("A1:A10").Value
            Case 2
                .List = Worksheets("Sheet3").Range("A1:A10").Value
        End Select
    End With
End Sub

Private Sub ComboBox1_Change()
    populate ComboBox1, ComboBox2
End Sub

Private Sub ComboBox3_Change()
    populate ComboBox3, ComboBox4
End Sub

Private Sub ComboBox5_Change()
    populate ComboBox5, ComboBox6
End Sub