Hi Antonio
Folgendes:
ListBox1.List(ListBox.ListIndex, 0) + 1
entspricht neu der ID des ausgewählten ListBox Eintrags d.h. es wird der Wert aus ListBox Spalte A ausgelesen.
Mit diesem Wert kannst du dann in der Tabelle suchen.
Weiter sehe ich noch folgende Unstimmigkeiten:
- ListIndex 0 (null) entspricht dem ersten ListBox Eintrag. -1 bedeutet dass nichts ausgewählt ist.
- Einmal lädst du die ListBox ohne Überschrift und einmal mit. Die Titel würde ich nicht in der ListBox darstellen.
- Wenn du die Tabelle Spalte A nicht zentrierst formatierst sieht du, dass die ID 2, 3 und 12 keine richtige Zahlen sind.
Private Sub ListBox1_Click() 'Eintrag an TextBox
Dim i As Byte, xZeile As Long
With ListBox1
For i = 1 To 11
If .ListIndex = -1 Then
Controls("TextBox" & i) = ""
Else
xZeile = Application.Match(CLng(.List(.ListIndex, 0)), Columns(1), 0)
Controls("TextBox" & i) = Cells(xZeile, i)
End If
Next i
End With
End Sub
Private Sub CommandButton1_Click() 'Löschen
Dim i As Byte
With ListBox1
If .ListIndex -1 Then
Rows(.List(.ListIndex, 0) + 1).Delete
For i = 1 To 11
Controls("TextBox" & i) = ""
Next i
UserForm_Initialize
End If
End With
End Sub
Private Sub CommandButton2_Click() 'Übernehmen
Dim xZeile As Long, i As Byte
If TextBox1 = "" Then Exit Sub
If WorksheetFunction.CountIf(Columns(1), CLng(TextBox1)) = 0 Then
xZeile = [A65536].End(xlUp).Row + 1
Else
xZeile = Application.Match(CLng(TextBox1), Columns(1), 0)
End If
For i = 1 To 11
Select Case i
Case 9, 10, 11
Cells(xZeile, i) = Format(Controls("TextBox" & i).Value, " ####0.00")
Case Else
Cells(xZeile, i) = Controls("TextBox" & i)
End Select
Controls("TextBox" & i) = ""
Next i
UserForm_Initialize
End Sub
Private Sub CommandButton3_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim L As Long
With ListBox1
.Clear
.ColumnCount = 5
.ColumnWidths = "30;30;100;300;100"
For L = 2 To Tabelle1.Cells(Rows.Count, 1).End(xlUp).Row
.AddItem Tabelle1.Cells(L, 1)
.List(.ListCount - 1, 1) = Tabelle1.Cells(L, 2)
.List(.ListCount - 1, 2) = Tabelle1.Cells(L, 3)
.List(.ListCount - 1, 3) = Tabelle1.Cells(L, 5)
.List(.ListCount - 1, 4) = Tabelle1.Cells(L, 6)
Next
.ListIndex = -1
End With
End Sub
Private Sub TextBox12_Change()
'Suchen
Dim lng As Long
Dim i As Integer
With ListBox1
.Clear
.ColumnCount = 5
.ColumnWidths = "30;30;100;300;100"
For lng = 2 To Tabelle1.Cells(Rows.Count, 1).End(xlUp).Row
If InStr(LCase(Cells(lng, 5).Value), LCase(TextBox12.Value)) > 0 Then
.AddItem Cells(lng, 1)
.Column(1, i) = Tabelle1.Cells(lng, 2)
.Column(2, i) = Tabelle1.Cells(lng, 3)
.Column(3, i) = Tabelle1.Cells(lng, 5)
.Column(4, i) = Tabelle1.Cells(lng, 6)
i = i + 1
End If
Next lng
End With
End Sub
cu
Chris