Anzeige
Archiv - Navigation
1732to1736
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Listbox Sortierung umkehren

Listbox Sortierung umkehren
16.01.2020 13:11:37
Curly
Hallo zusammen,
ich habe ein kleines problem, ausgangsposition ist das Thread https://www.herber.de/forum/cgi-bin/callthread.pl?index=1732266
Wenn ich die Userform Initialize,
Private Sub UserForm_Initialize()
Dim lngAnzahl As Long
With Me.ListBox1
.ColumnCount = 12
'.ColumnWidths = "60;150;60;60;100;100;60;60;"
.ColumnWidths = "1,83cm;6,1cm;3,5cm;1,5cm;3cm;1,8cm;1,8cm;0,7cm;1,8cm;0,7cm;1,8cm;0,7cm;"
End With
ListBox1.Clear
Dim lastrow As Integer
Dim avntValues As Variant
Dim ialngIndex As Long
With Tabelle1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
avntValues = .Range("A2:L" & lastrow).Value
End With
ListBox1.List = avntValues
Dim d As Long
With ComboBox1
For d = 2 To 3
.AddItem ThisWorkbook.Worksheets("Drop-Down").Cells(d, 4).Value ' Spalte D
.MatchRequired = True
.ListIndex = 0
Next d
End With
End Sub

löst er sofort das Change Ereignis der Combobox aus und liest somit die Listbox neu ein

Private Sub ComboBox1_Change()
Dim i As Long
Dim lngAnzahl As Long
Dim vArr
Dim lastrow As Integer
With Tabelle1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
vArr = .Range("A2:Q" & lastrow)
End With
ListBox1.Clear
For i = 1 To UBound(vArr)
If vArr(i, 16) = ComboBox1.Value Then
ListBox1.AddItem vArr(i, 1)
lngAnzahl = ListBox1.ListCount
ListBox1.List(lngAnzahl - 1, 1) = vArr(i, 2)
ListBox1.List(lngAnzahl - 1, 2) = vArr(i, 3)
ListBox1.List(lngAnzahl - 1, 3) = vArr(i, 4)
ListBox1.List(lngAnzahl - 1, 4) = vArr(i, 5)
ListBox1.List(lngAnzahl - 1, 5) = vArr(i, 6)
ListBox1.List(lngAnzahl - 1, 6) = vArr(i, 7) 'Datum
ListBox1.List(lngAnzahl - 1, 7) = vArr(i, 8)
ListBox1.List(lngAnzahl - 1, 8) = vArr(i, 9) 'Datum
ListBox1.List(lngAnzahl - 1, 9) = vArr(i, 10)
ListBox1.List(lngAnzahl - 1, 10) = vArr(i, 11) 'Datum
ListBox1.List(lngAnzahl - 1, 11) = vArr(i, 12)
If Not IsDate(vArr(i, 7)) Then ListBox1.List(lngAnzahl - 1, 6) = Empty
If Not IsDate(vArr(i, 9)) Then ListBox1.List(lngAnzahl - 1, 8)  = Empty
If Not IsDate(vArr(i, 11)) Then ListBox1.List(lngAnzahl - 1, 10)  = Empty
End If
Next i
End Sub

wenn ich danach die Sortierfunktion aufrufe , läuft er gegen einen Fehler.
Wenn ich mir dann im Lokalfenster das Array anschaue, sind alle Felder vom Typ Variant/String,
wenn ich das Change Ereignis ausklammere sind die Werte richtig definiert mit z.b. Variant/Date und die Sortierfunktion läuft.
Vielen Dank
Curly

8
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Listbox Sortierung umkehren
16.01.2020 13:20:56
Rudi
Hallo,
Option Explicit
Dim blnINIT as Boolean
Private Sub UserForm_Initialize()
Dim lngAnzahl As Long
Dim lastrow As Integer
Dim avntValues As Variant
Dim ialngIndex As Long
Dim d As Long
blnINIT=True
With Me.ListBox1
.ColumnCount = 12
'.ColumnWidths = "60;150;60;60;100;100;60;60;"
.ColumnWidths = "1,83cm;6,1cm;3,5cm;1,5cm;3cm;1,8cm;1,8cm;0,7cm;1,8cm;0,7cm;1,8cm;0,7cm;"
.Clear
End With
With Tabelle1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
avntValues = .Range("A2:L" & lastrow).Value
End With
ListBox1.List = avntValues
With ComboBox1
For d = 2 To 3
.AddItem ThisWorkbook.Worksheets("Drop-Down").Cells(d, 4).Value ' Spalte D
.MatchRequired = True
.ListIndex = 0
Next d
End With
blnINIT=False
End Sub
Private Sub ComboBox1_Change()
Dim i As Long
Dim lngAnzahl As Long
Dim vArr
Dim lastrow As Integer
If Not blnINIT Then
With Tabelle1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
vArr = .Range("A2:Q" & lastrow)
End With
ListBox1.Clear
For i = 1 To UBound(vArr)
If vArr(i, 16) = ComboBox1.Value Then
ListBox1.AddItem vArr(i, 1)
lngAnzahl = ListBox1.ListCount
ListBox1.List(lngAnzahl - 1, 1) = vArr(i, 2)
ListBox1.List(lngAnzahl - 1, 2) = vArr(i, 3)
ListBox1.List(lngAnzahl - 1, 3) = vArr(i, 4)
ListBox1.List(lngAnzahl - 1, 4) = vArr(i, 5)
ListBox1.List(lngAnzahl - 1, 5) = vArr(i, 6)
ListBox1.List(lngAnzahl - 1, 6) = vArr(i, 7) 'Datum
ListBox1.List(lngAnzahl - 1, 7) = vArr(i, 8)
ListBox1.List(lngAnzahl - 1, 8) = vArr(i, 9) 'Datum
ListBox1.List(lngAnzahl - 1, 9) = vArr(i, 10)
ListBox1.List(lngAnzahl - 1, 10) = vArr(i, 11) 'Datum
ListBox1.List(lngAnzahl - 1, 11) = vArr(i, 12)
If Not IsDate(vArr(i, 7)) Then ListBox1.List(lngAnzahl - 1, 6) = Empty
If Not IsDate(vArr(i, 9)) Then ListBox1.List(lngAnzahl - 1, 8)  = Empty
If Not IsDate(vArr(i, 11)) Then ListBox1.List(lngAnzahl - 1, 10)  = Empty
End If
Next i
End If
End Sub
Gruß
Rudi
Anzeige
AW: Listbox Sortierung umkehren
16.01.2020 14:40:56
Curly
Danke Rudi,
somit kann ich schonmal die ComboBox mit einem Textbelegen ohne das die Change Routine angestoßen wird.
Allerdings bleibt leider immer noch der Fehler mit der Sortierfunktion , wenn die ComboBox geändert wird :-(
Gruß Curly
AW: Listbox Sortierung umkehren
16.01.2020 15:24:30
Rudi
Hallo,
evtl. so?
Private Sub ComboBox1_Change()
Dim i As Long, j As Integer, n As Long
Dim lngAnzahl As Long
Dim vArr, arrList()
Dim lastrow As Integer
If Not blnINIT Then
With Tabelle1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
vArr = .Range("A2:Q" & lastrow)
ReDim arrList(1 To WorksheetFunction.CountIf(.Columns(16), ComboBox1.Value), 1 To 12)
End With
Listbox1.Clear
For i = 1 To UBound(vArr)
If vArr(i, 16) = ComboBox1.Value Then
n = n + 1
For j = 1 To 12
arrList(n, j) = vArr(i, j)
Next j
End If
Next i
Listbox1.List = arrList
End If
End Sub

Gruß
Rudi
Anzeige
AW: Listbox Sortierung umkehren
16.01.2020 18:16:55
Curly
Danke Rudi,
jetzt läufts rund .
Wünsch dir einen schönen Abend.
Gruß Curly
AW: Listbox Sortierung umkehren
17.01.2020 10:05:11
Curly
Guten Morgen zusammen,
ich müsste noch folgenden Code anpassen, allerdings weiß ich nicht genau wie die beste lösung u.a. für redim wäre
ich habe eine Textbox als Suchfeld, hier kann ein Datum, ein text oder eine KW (05/2020) zb. eingegeben werden.

Private Sub CommandButton1_Click()
Dim c As Range
Dim rngBereich As Range
Dim lngAnzahl As Long
Dim strFirst As String
Dim variante As Byte
ListBox1.Clear
If InStr(TextBox1.Value, ".") Then
variante = 1
ElseIf InStr(TextBox1.Value, "/") Then
variante = 2
Else
variante = 3
End If
If variante = 1 Or variante = 3 Then
With Sheets("Firmen")
Set rngBereich = .Columns("A:L")
If variante = 1 Then Set c = rngBereich.Find(What:=CDate(TextBox1.Value), LookIn:= _
xlValues, LookAt:=xlPart)
If variante = 3 Then Set c = rngBereich.Find(What:=TextBox1.Value, LookIn:=xlValues,  _
LookAt:=xlPart)
If Not c Is Nothing Then
strFirst = c.Address
Do
ListBox1.AddItem .Cells(c.Row, 1)
lngAnzahl = ListBox1.ListCount
ListBox1.List(lngAnzahl - 1, 1) = .Cells(c.Row, 2)
ListBox1.List(lngAnzahl - 1, 2) = .Cells(c.Row, 3)
ListBox1.List(lngAnzahl - 1, 3) = .Cells(c.Row, 4)
ListBox1.List(lngAnzahl - 1, 4) = .Cells(c.Row, 5)
ListBox1.List(lngAnzahl - 1, 5) = .Cells(c.Row, 6)
ListBox1.List(lngAnzahl - 1, 6) = .Cells(c.Row, 7)
ListBox1.List(lngAnzahl - 1, 7) = .Cells(c.Row, 8)
ListBox1.List(lngAnzahl - 1, 8) = .Cells(c.Row, 9)
ListBox1.List(lngAnzahl - 1, 9) = .Cells(c.Row, 10)
ListBox1.List(lngAnzahl - 1, 10) = .Cells(c.Row, 11)
ListBox1.List(lngAnzahl - 1, 11) = .Cells(c.Row, 12)
If Not IsDate(.Cells(c.Row, 7)) Then ListBox1.List(lngAnzahl - 1, 6) = Empty
If Not IsDate(.Cells(c.Row, 9)) Then ListBox1.List(lngAnzahl - 1, 8) = Empty
If Not IsDate(.Cells(c.Row, 11)) Then ListBox1.List(lngAnzahl - 1, 10) = Empty
Set c = rngBereich.FindNext(c)
Loop While Not c Is Nothing And c.Address  strFirst
End If
End With
end sub
Hier würde ich gerne meinen Code noch optimieren , sodass auch die sortierfunktion wieder läuft und das array passend dimensioniert ist.
Danke Curly
Anzeige
Listbox Sortierung - Keiner eine Idee? owT
17.01.2020 16:00:47
Curly
Hallo,
hat keiner eine Idee?
Gruß Curly
AW: Listbox Sortierung - Keiner eine Idee? owT
17.01.2020 16:42:52
Daniel
naja, so konkret hattest du ja auch noch keine Frage gestellt.
ich würde, da du hier mehr als 10 Spalten in die Listbox übernehemen willst (was meines Wissen nach mit .AddItem auch nicht möglich ist) , hier so vorgehen, zumal du die Spalten in unveränderter Reihenfolge und ohne Lücke übernehmen willst:
- sortiere die Excelliste nach der Suchspalte, so dass alle Einträge für die Listbox direkt untereinander stehen.
Dann kannst du den ganzen Zellbereich in einem Schritt in die Listbox übernehmen, und das mit beliebig vielen Spalten Listbox1.List = Range(Zelle_oben_links, Zelle_unten_rechts).Value
dann musst du auch nur die erste und letzte Zeile des gesuchten Wertes mit .Find ermitteln (dazu gibt's SerachDirection:=xlNext und xlPrevious)
Gruß Daniel
Anzeige
AW: Listbox Sortierung - Keiner eine Idee? owT
17.01.2020 17:49:46
Curly
sorry daniel,
Rudi , hat mir bei dem Code geholfen, wo, wenn sich die ComboBox ändert, das array neu eingelesen und dimensioniert wird.
Private Sub ComboBox1_Change()
Dim i As Long, j As Integer, n As Long
Dim lngAnzahl As Long
Dim vArr, arrList()
Dim lastrow As Integer
If Not blnINIT Then
With Tabelle1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
vArr = .Range("A2:Q" & lastrow)
ReDim arrList(1 To WorksheetFunction.CountIf(.Columns(16), ComboBox1.Value), 1 To 12)
End With
Listbox1.Clear
For i = 1 To UBound(vArr)
If vArr(i, 16) = ComboBox1.Value Then
n = n + 1
For j = 1 To 12
arrList(n, j) = vArr(i, j)
Next j
End If
Next i
Listbox1.List = arrList
End If
End Sub
und das wollte ich eben auf die 3 Möglichkeiten (wenn ein . vorkommt ist es ein Datum, wenn ein / ist es eine KW, sonst ist es ein text) der Textbox nun auch ändern, allerdings verzweifel ich gerade.
die Suche geht über 6 Spalten...
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige