VBA Dropdown (ComboBox)
#1
Ich Grüße euch meine Excelbrüder,

ich hab einige Schwierigkeiten mit VBA, falls es hier nicht reinpasst einfach Bescheid geben, dann pack ich das wo anders hin.

Ich möchte mit VBA ein DropDownButton(Kombinationsfeld) haben der folgende Eigenschaften besitzen soll:

-aus zwei Tabellen
-jeweils die Hersteller anzeigen, aber nur einmal
-die Tabelle soll auch dynamisch sein, sprich das man die Tabelle erweitern kann

Der zweite DropdownButton soll die Eigenschaft besitzen, dass er nur die Größen der jeweiligen Hersteller anzeigt die im ersten Dropdown ausgewählt wurden.

Da ich schriftlich mein Anliegen nicht immer gut ausdrücken kann, findet ihr im Anhang eine Bsp. Datei.
 
Schöne Grüße Joe Angel


Angehängte Dateien
.xlsm   DropDownBsp_VBA.xlsm (Größe: 25,92 KB / Downloads: 7)
Top
#2
Hallo Joe,

das könnte z. B. so funktionieren

Code:
Option Explicit

Private Sub ComboBox4_Change()

Dim MyDict  As Object
Dim WkSh    As Worksheet
Dim vTemp   As Variant
Dim rZelle  As Range
Dim sFundst  As String

  Set MyDict = CreateObject("Scripting.Dictionary")
  Set WkSh = ThisWorkbook.Worksheets("Tabellen")
   
  With WkSh.Columns(2)
     Set rZelle = .Find(What:=ComboBox4.Value, LookAt:=xlWhole, LookIn:=xlValues)
     If Not rZelle Is Nothing Then
        sFundst = rZelle.Address
        Do
           If WkSh.Range("C" & rZelle.Row).Value <> "" Then MyDict(WkSh.Range("C" & rZelle.Row).Value) = 0
           Set rZelle = .FindNext(rZelle) ' den evtl. nächsten Begriff suchen
        Loop While Not rZelle Is Nothing And rZelle.Address <> sFundst
     Else
        With WkSh.Columns(10)
           Set rZelle = .Find(What:=ComboBox4.Value, LookAt:=xlWhole, LookIn:=xlValues)
           If Not rZelle Is Nothing Then
              sFundst = rZelle.Address
              Do
                 If WkSh.Range("K" & rZelle.Row).Value <> "" Then MyDict(WkSh.Range("K" & rZelle.Row).Value) = 0
                 Set rZelle = .FindNext(rZelle) ' den evtl. nächsten Begriff suchen
              Loop While Not rZelle Is Nothing And rZelle.Address <> sFundst
           End If
        End With
     End If
  End With
 
  With ComboBox5
     .List = MyDict.keys
    '.ListIndex = 0
  End With

End Sub

Private Sub CommandButton2_Click()

  Unload Seilauslegung

End Sub


Private Sub UserForm_Initialize()

Dim MyDict  As Object
Dim vTemp   As Variant
Dim lZeile  As Long
   
   Set MyDict = CreateObject("Scripting.Dictionary")
   
   With ThisWorkbook.Worksheets("Tabellen")
      vTemp = .Range("B6:B" & .Cells(.Rows.Count, 2).End(xlUp).Row)
   End With
   
   For lZeile = LBound(vTemp) To UBound(vTemp)
      If vTemp(lZeile, 1) <> "" Then MyDict(vTemp(lZeile, 1)) = 0
   Next lZeile
   
   With ThisWorkbook.Worksheets("Tabellen")
      vTemp = .Range("J6:J" & .Cells(.Rows.Count, 10).End(xlUp).Row)
   End With
   
   For lZeile = LBound(vTemp) To UBound(vTemp)
      If vTemp(lZeile, 1) <> "" Then MyDict(vTemp(lZeile, 1)) = 0
   Next lZeile
   
   With ComboBox4
       .List = MyDict.keys
       '.ListIndex = 0
   End With
   
End Sub

Gruß Peter


Angehängte Dateien
.xlsm   Scripting Dictionary CoBos fuellen.xlsm (Größe: 32,51 KB / Downloads: 16)
[-] Folgende(r) 1 Nutzer sagt Danke an pefeu für diesen Beitrag:
  • Joe
Top
#3
Hallo Peter,

ich bin echt baff, Danke für deine Mühe genau das was ich wollte :)

Schöne Grüße Joe
Top
#4
(01.10.2015, 10:58)Joe schrieb: Hallo Peter,

ich bin echt baff, Danke für deine Mühe genau das was ich wollte :)

Schöne Grüße Joe

Hallo Joe,

danke für die Rückmeldung.

Gruß Peter
Top
#5
Ich möchte vorschlagen:


Code:
Private Sub UserForm_Initialize()
     sn = Tabelle2.ListObjects("Tabelle1").DataBodyRange.Columns(1).Resize(, 2)
     sq = Tabelle2.ListObjects("Tabelle2").DataBodyRange.Columns(1).Resize(, 2)
     
     With CreateObject("Scripting.Dictionary")
        For j = 1 To UBound(sn)
          x0 = .Item(sn(j, 1))
          If InStr(c00, sn(j, 1) & "_" & sn(j, 2)) = 0 Then c00 = c00 & "|" & sn(j, 1) & "_" & sn(j, 2)
        Next
        For j = 1 To UBound(sq)
          x0 = .Item(sq(j, 1))
           If InStr(c00, sq(j, 1) & "_" & sq(j, 2)) = 0 Then c00 = c00 & "|" & sq(j, 1) & "_" & sq(j, 2)
        Next
        ComboBox4.List = .keys
        ComboBox4.Tag = c00
     End With
End Sub

Private Sub ComboBox4_Change()
   ComboBox5.List = Split(Replace(Join(Filter(Split(ComboBox4.Tag, "|"), ComboBox4.Value), "|"), ComboBox4.Value & "_", ""), "|")
End Sub

Private Sub CommandButton2_Click()
   Unload Seilauslegung
End Sub
Top
#6
Oder:

Code:
Private Sub UserForm_Initialize()
     sn = Tabelle2.ListObjects("Tabelle1").DataBodyRange.Columns(1).Resize(, 2)
     sq = Tabelle2.ListObjects("Tabelle2").DataBodyRange.Columns(1).Resize(, 2)
     
     With CreateObject("Scripting.Dictionary")
        For j = 1 To UBound(sn)
          x0 = .Item(sn(j, 1))
          x0 = .Item(sn(j, 1) & "_" & sn(j, 2))
        Next
        For j = 1 To UBound(sq)
          x0 = .Item(sq(j, 1))
          x0 = .Item(sq(j, 1) & "_" & sq(j, 2))
        Next
        ComboBox4.List = Filter(.keys, "_", False)
        ComboBox4.Tag = Join(Filter(.keys, "_"), "|")
     End With
End Sub

Private Sub ComboBox4_Change()
   ComboBox5.List = Split(Replace(Join(Filter(Split(ComboBox4.Tag, "|"), ComboBox4), "|"), ComboBox4 & "_", ""), "|")
End Sub

Private Sub CommandButton2_Click()
   Unload Me
End Sub
[-] Folgende(r) 1 Nutzer sagt Danke an snb für diesen Beitrag:
  • Joe
Top
#7
Hey snb,

Danke für dein Vorschlag habe aber schon den Code drin, nächste mal probier ich dein Code.

Gruß Joe
Top
#8
(02.10.2015, 10:29)Joe schrieb: Hey snb,

Danke für dein Vorschlag habe aber schon den Code drin, nächste mal probier ich dein Code.

Gruß Joe
Top
#9
(01.10.2015, 22:40)snb schrieb: Oder:

Code:
Private Sub UserForm_Initialize()
     sn = Tabelle2.ListObjects("Tabelle1").DataBodyRange.Columns(1).Resize(, 2)
     sq = Tabelle2.ListObjects("Tabelle2").DataBodyRange.Columns(1).Resize(, 2)
     
     With CreateObject("Scripting.Dictionary")
        For j = 1 To UBound(sn)
          x0 = .Item(sn(j, 1))
          x0 = .Item(sn(j, 1) & "_" & sn(j, 2))
        Next
        For j = 1 To UBound(sq)
          x0 = .Item(sq(j, 1))
          x0 = .Item(sq(j, 1) & "_" & sq(j, 2))
        Next
        ComboBox4.List = Filter(.keys, "_", False)
        ComboBox4.Tag = Join(Filter(.keys, "_"), "|")
     End With
End Sub

Private Sub ComboBox4_Change()
   ComboBox5.List = Split(Replace(Join(Filter(Split(ComboBox4.Tag, "|"), ComboBox4), "|"), ComboBox4 & "_", ""), "|")
End Sub

Private Sub CommandButton2_Click()
   Unload Me
End Sub

Hallo snb

wenn du schon eine Lösung vorstellst, sollte die so komplett sein, dass auch alle verwendeten Variablen deklariert sind!

Dies nur als Vorschlag.

Gruß Peter
Top
#10
@Pefeu

Das wäre 100% redundant.
Top


Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste