Filter für Listbox
#1
Hallo an alle.

Ich habe ein UserForm mit einer Combobox als Liste (Werte = offen/erledigt)

Dazu habe ich eine Tabelle (Spalte 19) in der ein Datum steht, wenn erledigt, sonst ist das leer

Wie bekomme ich es jetzt hin, dass er nach Combobox-Wert filtert

Code:
Private Sub cbStatus_Change()

cbHaus.Value = ""
cbJahr.Value = ""
TextBox1.Value = ""

Dim Zeile As Long
Me.ListBox1.Clear

'If Me.cbStatus.Value = "offen" Then
'Statusselect = ""
'Else
'Statusselect = ""
'End If

For Zeile = 2 To Tabelle1.Cells(Rows.Count, 1).End(xlUp).Row

    If InStr(1, Tabelle1.Cells(Zeile, 19).Value, Statusselect) <> 0 Then
   
        Me.ListBox1.AddItem Format(Tabelle1.Cells(Zeile, 1).Value, "0000")
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Tabelle1.Cells(Zeile, 2).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 2) = Tabelle1.Cells(Zeile, 4).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 3) = Tabelle1.Cells(Zeile, 5).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 4) = Tabelle1.Cells(Zeile, 3).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = Tabelle1.Cells(Zeile, 19).Value

    End If

Next Zeile

End Sub

 Danke für Eure Hilfe
Antworten Top
#2
Hallöchen,

was soll denn gefiltert werden? Was auf dem Blatt oder der Inhalt der Listbox? Nach welcher Combobox?
Bei der Listbox kannst Du einfach die Bedingung davor setzen, im Prinzip in jeder Zeile:

If Tabelle1.Cells(Zeile, 2).Value <> cb... Then Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Tabelle1.Cells(Zeile, 2).Value
.      \\\|///      Hoffe, geholfen zu haben.
       ( ô ô )      Grüße, André aus G in T  
  ooO-(_)-Ooo    (Excel 97-2019+365)
Antworten Top
#3
Combobox = cbStatus

RowSource = tblStatus 

tblStatus = offen und erledigt

in Tabelle1 befinden Sich die Daten, die in der ListBox1 angezeigt werden.

in Spalte 19 der Tabelle1 sind das erledigte datum hinterlegt, bzw. wenn offen dann kein datum (leer)

Klick ich jetzt die Combobox auf offen, soll er mir nur die Einträge in der ListBox1 auflisten, wo kein Datum in Spalte 19 steht.

Klick ich jetzt die Combobox auf erledigt, soll er mir nur die Einträge in der ListBox1 auflisten, wo ein Datum in Spalte 19 steht.
Antworten Top
#4
Da müsstest Du auf der linken Seite der Formel aus der 2 eine 19 machen.
.      \\\|///      Hoffe, geholfen zu haben.
       ( ô ô )      Grüße, André aus G in T  
  ooO-(_)-Ooo    (Excel 97-2019+365)
Antworten Top
#5
If Tabelle1.Cells(Zeile, 2).Value <> cbStatus Then 
Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Tabelle1.Cells(Zeile, 2).Value 


Da wird er nichts finden mit der Formel!

cbStatus hat nur den Wert offen oder erledigt.

in der Spalte 19 der Tabelle1 gibt es diese Werte nicht.

Da steht ein Datum drin oder es ist leer.

Er müsste wie folg suchen:

ist cbStatus = "offen" schau nach Zeilen, wo kein Datum in Spalte 19 steht
ist cbStatus ="erledigt" schau nach zeilen, wo ein Datum in Spalte 19 steht

So funktioniert es erstmal:
Code:
Private Sub cbStatus_Change()

cbHaus.Value = ""
cbJahr.Value = ""
TextBox1.Value = ""

Dim Zeile As Long
Dim Statusselect As String
Me.ListBox1.Clear

If Me.cbStatus.Value = "offen" Then

For Zeile = 2 To Tabelle1.Cells(Rows.Count, 1).End(xlUp).Row
   
    If IsDate(Tabelle1.Cells(Zeile, 19).Value) = 0 Then
   
        Me.ListBox1.AddItem Format(Tabelle1.Cells(Zeile, 1).Value, "0000")
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Tabelle1.Cells(Zeile, 2).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 2) = Tabelle1.Cells(Zeile, 4).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 3) = Tabelle1.Cells(Zeile, 5).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 4) = Tabelle1.Cells(Zeile, 3).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = Tabelle1.Cells(Zeile, 19).Value

    End If

Next Zeile

End If

If Me.cbStatus.Value = "erledigt" Then

For Zeile = 2 To Tabelle1.Cells(Rows.Count, 1).End(xlUp).Row
   
    If IsDate(Tabelle1.Cells(Zeile, 19).Value) <> 0 Then
   
        Me.ListBox1.AddItem Format(Tabelle1.Cells(Zeile, 1).Value, "0000")
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = Tabelle1.Cells(Zeile, 2).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 2) = Tabelle1.Cells(Zeile, 4).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 3) = Tabelle1.Cells(Zeile, 5).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 4) = Tabelle1.Cells(Zeile, 3).Value
        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = Tabelle1.Cells(Zeile, 19).Value

    End If

Next Zeile

End If

End Sub
Antworten Top
#6
Hallo,

falls es dich noch interessiert, hier mal mein Vorschlag via Array. Dies ist wesentlich schneller als der Weg via .AddItem

ins Initialize des Userforms:
Code:
cbStatus.List = Array("offen", "erledigt")

und:
Code:
Private Sub cbStatus_Change()
    Dim i&, j&, k&, arrL(), arrT()
    arrT = Application.Index(Tabelle1.ListObjects(1).DataBodyRange.Value, Evaluate("row(1:" & Tabelle1.ListObjects(1).ListRows.Count & ")"), Array(1, 2, 4, 5, 3, 19))
    arrL = arrT
    If cbStatus.ListIndex = 1 Then
        For i = 1 To UBound(arrT)
            If IsDate(arrT(i, 6)) Then
                k = k + 1
                arrL(k, 1) = Format(arrT(i, 1), "0000")
                For j = 2 To 6
                    arrL(k, j) = arrT(i, j)
                Next j
            End If
        Next
    Else
        For i = 1 To UBound(arrT)
            If Not IsDate(arrT(i, 6)) Then
                k = k + 1
                arrL(k, 1) = Format(arrT(i, 1), "0000")
                For j = 2 To 6
                    arrL(k, j) = arrT(i, j)
                Next j
            End If
        Next
    End If
    If cbStatus.ListIndex = -1 Then
        ListBox1.List = arrT
        For i = 0 To ListBox1.ListCount - 1
            ListBox1.List(i, 0) = Format(ListBox1.List(i, 0), "0000")
        Next i
    Else
        ListBox1.List = Application.Index(arrL, Evaluate("row(1:" & k & ")"), Array(1, 2, 3, 4, 5, 6))
    End If
End Sub
Du nutzt O2024. Da kann man dies über die Funktion Filter die Listbox füllen. Das ist ebenfalls eine sehr effiziente Möglichkeit.

Gruß Uwe
Antworten Top


Gehe zu:


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