Der in dem Userform hinterlegte Code.
Option Explicit
'Modulweite Variablen deklarieren
Const C_mstrDatenblatt As String = "Tabelle1"
Const C_mstrZielblatt As String = "Tabelle2"
Dim mobjDic As Object
Dim mlngLast As Long
Dim mlngZ As Long
Private Sub ComboBox1_Enter()
'Erste Combobox. Jedes Land in Spalte A wird einmalig angezeigt
Set mobjDic = CreateObject("Scripting.Dictionary")
For mlngZ = 2 To mlngLast
mobjDic(Worksheets(C_mstrDatenblatt).Cells(mlngZ, 1).Value) = 0
Next
Me.ComboBox1.List = mobjDic.keys
Set mobjDic = Nothing
End Sub
Private Sub ComboBox2_Enter()
'Zweite Combobox in Abhängigkeit von Combobox1.
'Jeder passende Stadt in Spalte B wird einmalig angezeigt.
Set mobjDic = CreateObject("Scripting.Dictionary")
With Worksheets(C_mstrDatenblatt)
For mlngZ = 2 To mlngLast
If .Cells(mlngZ, 1).Value = Me.ComboBox1.Value Then
mobjDic(.Cells(mlngZ, 2).Value) = 0
End If
Next
End With
Me.ComboBox2.List = mobjDic.keys
Set mobjDic = Nothing
End Sub
Private Sub ComboBox3_Enter()
'Dritte Combobox in Abhängigkeit von Combobox 1 + 2.
Me.ComboBox3.Clear
With Worksheets(C_mstrDatenblatt)
For mlngZ = 2 To mlngLast
If .Cells(mlngZ, 1).Value = Me.ComboBox1.Value And .Cells(mlngZ, 2).Value = Me.ComboBox2.Value Then
Me.ComboBox3.AddItem .Cells(mlngZ, 3).Value
End If
Next
End With
End Sub
Private Sub CommandButton1_Click()
'Userform schliessen
Unload Me
End Sub
Private Sub CommandButton2_Click()
'Ausgewählte Daten in Tabelle 2 übertragen
With Worksheets(C_mstrZielblatt)
.Cells(.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Value = Me.ComboBox1.Value
.Cells(.Cells(Rows.Count, 2).End(xlUp).Row + 1, 2).Value = Me.ComboBox2.Value
.Cells(.Cells(Rows.Count, 3).End(xlUp).Row + 1, 3).Value = Me.ComboBox3.Value
End With
End Sub
Private Sub UserForm_Initialize()
'Bei Start der Userform wird die unterste Zeile in Spalte A ermittelt
mlngLast = Worksheets(C_mstrDatenblatt).Cells(Rows.Count, 1).End(xlUp).Row
End Sub