Excel VBA通​​过带有选择案例语句的组合框循环

时间:2018-08-17 13:14:13

标签: excel vba loops switch-statement

我有一个带有6个Activex组合框的工作表。

Combobox1有21个choiche。 Combobox2取决于Combobox1,选择数量有所不同。

Combobox3有2个选择。 Combbox4依赖于Combobox3,有21种选择。

Combobox5有21个choiche。 Combobox6取决于Combobox5,选择的数量有所不同。

我想遍历combobox1-value1和combobox2-value1。

然后,我想遍历组合框3-值1和组合框4-值1。

我想遍历组合框5-值1和组合框6-值1。

我正在使用vlookup基于不同组合框的链接单元格。我目前拥有的代码仅循环通过combobox1和combobox2的单元格值。我想物理地更改组合框中的值,从value1更改为lastvalue。

这将是组合框1-值1,组合框2的值1到最后一个值,组合框3的第一个值,组合框4-值1,组合框5,值1,最后是组合框6-值1到最后一个值。

Sub Demo()
    Dim Ws As Worksheet
    Dim shp As Shape
    Dim cb As ComboBox

    Set Ws = ActiveSheet

    For Each shp In Ws.Shapes
        With shp
            Select Case .Type
                Case msoFormControl
                    If .FormControlType = xlDropDown Then
                        If .ControlFormat.Value = 0 Then
                            MsgBox .Name & " = "
                        Else
                            MsgBox .Name & " = " & .ControlFormat.List(.ControlFormat.Value)
                        End If
                    End If
                Case msoOLEControlObject
                    If .OLEFormat.progID = "Forms.ComboBox.1" Then
                        Set cb = .OLEFormat.Object.Object
                        MsgBox cb.Name & " = " & cb.Value
                    End If
            End Select
        End With
    Next
End Sub

上面的代码为我提供了6个Activex组合框的值。

 Sub try()
    Dim Ws As Worksheet
    Set Ws = Worksheets("Sheet1")
    Count = 0

    For Each OleObj In Ws.OLEObjects
        If OleObj.OLEType = xlOLEControl Then
            If TypeName(OleObj.Object) = "ComboBox" Then
                Count = Count + 1
            End If
        End If
    Next OleObj

    MsgBox "Number of ComboBoxes :" & Count

    End Sub

此代码计算工作表中组合框的数量!也许可以修改它以增加每个组合框?

我正在考虑这样的事情:

Sub Test()

Select Case Me.Form
    Case "Stockholms län"
        Me.Kommun1.RowSource = "Stockholms_län"
        ' Code for each loop where combobox1 is "Stockholm län" and Combobox2  
        ' is a named range.
    Case "Skåne län"
        Me.Kommun1.RowSource = "Skåne_län"
        ' Code for each loop where combobox1 is "Skåne län" and Combobox2 is 
        ' a named range.


End Select
End sub

我可以手动设置combobox1和combobox2的值。

Sub WantToLoop()
    Dim län1 As String
    Dim kommun1 As String

    Dim län2 As String
    Dim kommun2 As String

ThisWorkbook.Sheets("Test").län1 = "Skåne län"
ThisWorkbook.Sheets("Test").kommun1 = "Bjuv"

End Sub

上面的代码可以工作,但是我无法为数百种选择选择案例。我该如何循环?

我越来越近了,但是现在我设置了组合框的值。我想访问组合框的值。

Sub try()

Dim i As Integer
Dim j As Integer

For i = 1 To 2
   Sheets("Test").Shapes("Län" & i).OLEFormat.Object.Object = "item" & i
   Sheets("Test").Shapes("Kommun" & i).OLEFormat.Object.Object = "item" & i

Next

End sub

Sub IterateComboBox()
Dim i As Long
  With Sheets("Jämföra").Län1
    For i = 0 To .ListCount - 1
    'Debug.Print .List(i)
    .Value = .List(i)
    Next
  End With
End Sub

这段代码可以满足我的要求。如何将其变成精选案例?

Sub Try2()
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim n As Integer

Sheets("Test").Län1.ListIndex = 0

For l = 0 To 25
Sheets("Test").Kommun1.ListIndex = l

Sheets("Test").Län2.ListIndex = 0

For n = 0 To 25
Sheets("Test").Kommun2.ListIndex = n

Application.ScreenUpdating = True


Sheets("Score").Select

Dim LR As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row + 1

'Län1, Kommun1 OK
Cells(LR, "A").Value = Sheets("Test").Range("G5").Value
Cells(LR, "B").Value = Sheets("Test").Range("G6").Value
Next
Next
End Sub

此代码循环通过combobox1 value1,combobox2所有值,combobox3 value1和combobox4所有值。我怎样才能把它变成箱子?

或者如何将其转换为函数,可以将Län1,Kommun1,Län2和Kommun2的值传递给函数?

0 个答案:

没有答案