2016-07-23 3 views
1

나는 여러 개의 종속적 인 Comboboxes를 가진 userform을 가지고있다. Comboboxes Change 이벤트에 다음 코드를 추가하고 싶습니다. 코딩해야 할 콤보 박스는 번호가 11에서 20 (Combobox11, Combobox 12 등)이며 종속 콤보 박스는 21에서 30으로 번호가 매겨집니다.코드 comboboxes on excel userform

코드를 10 번 복사하여 붙여 넣은 다음 해당 콤보 박스 번호

이것을 달성하기 위해 콤보 상자를 통해 루프를 사용하는 방법이 있습니까? 도움이된다면 가장 감사 할 것입니다.

Private Sub ComboBox11_Change() 

Dim index As Integer 
index = ComboBox11.ListIndex 
ComboBox21.Clear 

Select Case index 
    Case Is = 0 
     With ComboBox21 
      .RowSource = Range("SubCat1").Address(external:=True) 
     End With 

    Case Is = 1 
     With ComboBox21 
      .RowSource = Range("SubCat6").Address(external:=True) 
     End With 

    Case Is = 2 
     With ComboBox21 
      .RowSource = Range("SubCat7").Address(external:=True) 
     End With 

    Case Is = 3 
     With ComboBox21 
      .RowSource = Range("SubCat8").Address(external:=True) 
     End With 

    Case Is = 4 
     With ComboBox21 
      .RowSource = Range("SubCat9").Address(external:=True) 
     End With 

    'and several more case options 

End Select 

End Sub 

답변

0

이 클래스에 사용자 양식의 각 ComboBox 컨트롤을 설정하는 클래스 모듈 및 User_Init 서브를 사용할 수 있습니다.

내 코드에서 Main_Form을 User_Form의 이름으로 사용하고 User_Form 이름에 따라 코드를 수정하십시오.

는이 모듈을 호출 추가하고, 클래스 1에 다음 코드를 추가합니다

이 코드는 아래 User_Form_Init 간다

(내 코드에서 User_Form의 이름홈페이지 - 양식입니다)

Public WithEvents ComboBoxEvents As MSForms.ComboBox ' anytime a Change event occurs to any ComboBox, the Sub is triggered Private Sub ComboBoxEvents_Change() Dim ComboBox_Index As String Dim index As Integer With ComboBoxEvents ' read the index of the ComboBox, as long as the names remain ComboBox1, ComboBox2, ComboBox3, etc... ComboBox_Index = Mid(.Name, 9) ' run this code if it's ComboBox 11 to 20 If ComboBox_Index >= 11 And ComboBox_Index <= 20 Then index = .ListIndex Select Case index Case Is = 0 With Main_Form.Controls("ComboBox" & ComboBox_Index + 10) .RowSource = Range("SubCat1").Address(external:=True) End With Case Is = 1 With Main_Form.Controls("ComboBox" & ComboBox_Index + 10) .RowSource = Range("SubCat6").Address(external:=True) End With Case Is = 2 With Main_Form.Controls("ComboBox" & ComboBox_Index + 10) .RowSource = Range("SubCat7").Address(external:=True) End With Case Is = 3 With Main_Form.Controls("ComboBox" & ComboBox_Index + 10) .RowSource = Range("SubCat8").Address(external:=True) End With Case Is = 4 With Main_Form.Controls("ComboBox" & ComboBox_Index + 10) .RowSource = Range("SubCat9").Address(external:=True) End With 'and several more case options End Select End If End With End Sub 
:

Option Explicit 

Dim ComboBoxes() As New Class1 

Private Sub UserForm_Initialize() 

    Dim ComboBoxCounter As Integer, Obj As Control 

    For Each Obj In Me.Controls 
     If TypeOf Obj Is MSForms.ComboBox Then 
      ComboBoxCounter = ComboBoxCounter + 1 
      ReDim Preserve ComboBoxes(1 To ComboBoxCounter) 
      Set ComboBoxes(ComboBoxCounter).ComboBoxEvents = Obj 
     End If 
    Next Obj 

    Set Obj = Nothing 

End Sub 
+0

의 당신이 UserForm_Initialize 절차를 의미합니까. 거기에 코드를 넣었지만 사용자 폼을로드했을 때 콤보 상자에 항목이 없었습니다. – willi

+0

@willi'Set Obj = Nothing' 라인 다음에 위 코드에'UserForm_Initialize'의 원본 코드를 추가해야합니다. –

+0

코드를 UserForm_ initialize 프로 시저에 추가했지만 Option Explicit 또는 Dim 문 "ComboBoxCounter As Integer, Obj As Control"을 하위 이름 위에 두지 않습니다. 내가 뭘 놓치고 있니? 나는 며칠 동안 사무실에서 나갈 것이다. – willi

0

방법은 클래스 모듈을 추가하고 후 이름을

클래스

을 사용하고 있습니다

Option Explicit 

Public WithEvents Cmb As MSForms.ComboBox 

Private Sub Cmb_Change() 
    Dim index As Long 

    With Cmb 
     index = .ListIndex 
     With .Parent.Controls("ComboBox" & Mid(.Name, 9) + 10) 
      .Clear 
      Select Case index 
       Case 0 
        .RowSource = Range("SubCat1").Address(external:=True) 
       Case 1 To 4 
        .RowSource = Range("SubCat" & index + 5).Address(external:=True) 
      End Select 
     End With 
    End With 
End Sub 

이 그런 다음 userfom 코드 창으로 전환하고이 코드를 추가합니다 :

"을 CmbBox는"클래스 코드 창에 다음 코드를 추가

을 (당신은 어떤 이름을 선택하지만, 그것과 일치 할 수 있습니다)

Dim Cmbs(1 To 10) As New CmbBox '<--| this must be at the very top of your userform code pane 

Sub Userform_Initialize() 
    Dim i As Long 

    With Me.Controls 
     For i = 11 To 20 
      Set Cmbs(i - 10).Cmb = .Item("ComboBox" & i) 
     Next i 
    End With 
End Sub 

하고 코드가 User_Form_Init로 전환 말할 때 그것은 그것에게

+0

@willi :이 솔루션을 사용해 보셨습니까? 꽤 짧습니다 -> maintanable – user3598756

+0

@ 윌리 : 노력하고있는 사람들에게 피드백을주는 것이 좋을 것입니다. – user3598756

관련 문제