2017-12-04 3 views
0

콤보 상자 인 userform을 만들었습니다. 사용자가 선택한 결과에 따라 새로운 콤보 상자가 선택 가능한 새로운 선택 항목과 함께 나타납니다. 다음은 내가 시도한 최신 테스트입니다.Excel VBA Userform - 동적 콤보 상자에서 무언가가 변경되면 Sub를 실행합니다.

어떻게 사용자가 새로운 콤보 상자 값을 변경할 때 그것이 하위 미리 만들어진 기능을/실행할 수 있도록해야합니까

양식의 코드

Dim WB As Workbook 
Dim structSheet As Worksheet 
Dim tbCollection As Collection 


Private Sub UserForm_Activate() 
    Dim ignoreList(3) As String 

    ignoreList(0) = "main" 
    ignoreList(1) = "configurator" 
    ignoreList(2) = "create structure" 
    Set WB = Excel.ActiveWorkbook 

    For Each sheet In WB.Worksheets 
     If Not isInTable(ignoreList, sheet.Name) Then 
      supercode_box.AddItem sheet.Name 
     End If 
    Next 
End Sub 

Private Sub supercode_box_Change() 
    If Not sheetExists(supercode_box.text) Then Exit Sub 
    Set structSheet = WB.Worksheets(supercode_box.text) 

    'Dim obj As clsControlBox 

    topPos = 10 
    leftPos = 54 
    ID = 1 

    ' For ID = 1 To 2 
     Set ComboBox = createProductForm.Controls.add("Forms.ComboBox.1") 
     With ComboBox 
      .Name = "comboBoxName" 
      .Height = 16 
      .Width = 100 
      .Left = leftPos 
      .Top = topPos + ID * 18 
      .AddItem "test" 
      .Object.Style = 2 
     End With 

     Set tbCollection = New Collection 
     tbCollection.add ComboBox 

    'Next ID 
End Sub 

코드 Class1의 모듈

Private WithEvents MyTextBox As MSForms.controlBox 

Public Property Set Control(tb As MSForms.controlBox) 
    Set MyTextBox = tb 
    MsgBox ("did it get here?") 
End Property 

Public Sub comboBoxName_Change() 
    MsgBox ("start working ffs") 
End Sub 

Public Sub comboBoxName() 
    MsgBox ("?? maybe this?") 
End Sub 

답변

0

코드를 판단하는 가장 쉬운 방법은 필요한 값을 별도의 워크 시트에 작성하는 것입니다.

그런 다음 변경되었는지 확인하고 변경된 경우 새 값을 작성하고 원하는 절차를 시작하십시오. 한마디로

, 이런 식으로 뭔가 :

Sub TestMe() 

    If Worksheets("Special").Cells(1, 1) = WB.Worksheets(supercode_box.Text) Then 
     Call TheSpecificSub 
    End If 

    Worksheets("Special").Cells(1, 1) = WB.Worksheets(supercode_box.Text) 

End Sub 
+0

어떻게 그 날을 도움이 되나요? ComboBox에 TestMe() 서브를 추가하려면 어떻게합니까? –

관련 문제