2017-11-07 1 views
-1

Excel 사용자 정의 폼에 두 개의 콤보 상자가 있으며 두 번째 콤보 상자에 나열된 내용에 영향을주는 첫 번째 콤보 상자가 필요합니다. 내가 시트에서 Excel에서 데이터를 가져 오는 테이블은 연결된 이미지처럼 보입니다.첫 번째 콤보 상자를 기반으로 두 번째 콤보 상자를 채우십시오.

enter image description here.

데이터는 상호명 및 개인과 함께 사용됩니다. combobox2에 대한 설명은 DT 제한이 combobox1에서 선택되었다고 말하면 combobox2은 선택 사항으로 존과 스티브 만 표시합니다.

누구든지 도움을받을 수 있습니까?

답변

0
Private Sub UserForm_Initialize() 
With ComboBox1 'Your first combobox 
.Clear 


.AddItem "Users" 
.AddItem "Salary" ' Add main combobox Category 

End With 
End Sub 



Private Sub ComboBox1_Change() 

Dim index As Integer 
index = ComboBox1.ListIndex 

ComboBox2.Clear ' clear your dependent combobox 

Select Case index 
    Case Is = 0 ' Add Subcategory Display items 
     With ComboBox2 
     .AddItem "John" 
     .AddItem "Angie" 
     .AddItem "Sam" 
    End With 
Case Is = 1 
    With ComboBox2 
     .AddItem "20000" 
     .AddItem "45000" 
     .AddItem "80000" 
    End With 

End Select 

End Sub 
+0

위에서 언급 한 문제는 새로운 비즈니스와 개인이 해당 비즈니스에 연결된 상태에서 지속적으로 추가된다는 것입니다. 내가 자동으로 선택할 수있는 콤보 상자가 필요합니다, 항목 수가 고정되어 있지 않습니다 –

+0

@benjaminsolanke 그냥'ComboBox2.Clear' 라인을 추가하십시오. 무료로 당신을 도우려는 사람들과 잘 지내십시오. –

+0

@ AK47 요청한 사람이 자신의 코드를 개발하려는 노력을하지 않는다면 코드를 제공해서는 안됩니다. –

0

이 코드는 귀하의 제안을 해결합니다.

Private Sub UserForm_Initialize() 
Dim RowMax As Integer 
Dim wsh As Worksheet 
Dim countExit As Integer 
Dim CellCombo1 As String 
Dim i As Integer 
Dim j As Integer 

Set wsh = ThisWorkbook.Sheets("Sheet2") 
RowMax = wsh.Cells(Rows.Count, "A").End(xlUp).Row 
'find last row of sheet in column A 

ComboBox1.Clear 
'clear all value of comboBox1 

With ComboBox1 

    For i = 2 To RowMax 
    'Run each row of column A 

    countExit = 0 
    CellCombo1 = wsh.Cells(i, "A").Value 

      For j = i To 2 Step -1 
      'just show value not duplicate 

      If CellCombo1 = wsh.Cells(j, "A").Value Then 
      countExit = countExit + 1 
      End If 
      Next j 
     If countExit = 0 Then 

     ElseIf countExit > 1 Then 
      Else 
      .AddItem CellCombo1 
      End If 
    Next i 

End With 

End Sub 
Private Sub ComboBox1_Change() 
Dim RowMax As Integer 
Dim wsh As Worksheet 
Dim countExit As Integer 
Dim CellCombo2 As String 
Dim i As Integer 

Set wsh = ThisWorkbook.Sheets("Sheet2") 
RowMax = wsh.Cells(Rows.Count, "A").End(xlUp).Row 
'find last row of sheet in column A 

ComboBox2.Clear 
'clear all value of comboBox2 

With ComboBox2 
    For i = 2 To RowMax 
     If wsh.Cells(i, "A").Value = ComboBox1.Text Then 
     'Just show value of mapping with column A 
     .AddItem wsh.Cells(i, "B").Value 
     Else 
     End If 
    Next i 
End With 
End Sub 
관련 문제