Ich habe eine UserForm gebastelt, dabei mich SEHR stark an Online-Lösungen orientiert. Nun müsste ich diese noch etwas anpassen, komme allerdings mit der Umsetzung nicht mehr voran.
Code liegt bei, folgende Dinge bräuchte ich noch:
- Alle TextBox (ausser Spalte 1,2) sind Zahlen, werden aber noch als Text ins Excelsheet eingefügt. Diese müssen alle als Zahlen eingefügt resp. umgewandelt werden, da danach Formeln diese weiterverarbeiten. Habe zwar Scripts gefunden, brings aber nicht zum laufen..
- Ausserdem würde ich gerne die ListBox resp. meine Tabelle im Excel jeweils bei 'neuem Eintrag' oder 'Öffnen der Datei' alphabetisch sortieren (zuerste nach Gruppe in Spalte 1, danach nach Städtename in Spalte 2) resp. diese Sortierung überprüfen. Hier gibts zwar auch jede Menge Scripts zu finden, bringe diese aber nicht zum laufen.. da ich im Excel selbst mit Index/Vergleich arbeite, ist die Sortierung der Tabelle ja kein Problem, wäre einfach einiges angenehmer, wenns jeweils automatisch passieren würde.
Herzlichen Dank für jede Hilfe :)
Option Explicit
Option Compare Text
Private Const iCONST_ANZAHL_EINGABEFELDER As Integer = 23
Private Const lCONST_STARTZEILENNUMMER_DER_TABELLE As Long = 2
' EREIGNISROUTINEN DER USERFORM
Private Sub CommandButton1_Click()
Call EINTRAG_ANLEGEN
End Sub
Private Sub CommandButton2_Click()
Call EINTRAG_LOESCHEN
End Sub
Private Sub CommandButton3_Click()
Call EINTRAG_SPEICHERN
End Sub
Private Sub CommandButton4_Click()
Unload Me
End Sub
Private Sub ListBox1_Click()
Call EINTRAG_LADEN_UND_ANZEIGEN
End Sub
Private Sub UserForm_Activate()
If ListBox1.ListCount > 0 Then ListBox1.ListIndex = 0
End Sub
Private Sub UserForm_Initialize()
Call LISTE_LADEN_UND_INITIALISIEREN
End Sub
' VERARBEITUNGSROUTINEN
Private Sub LISTE_LADEN_UND_INITIALISIEREN()
Dim lZeile As Long
Dim lZeileMaximum As Long
Dim i As Integer
For i = 1 To iCONST_ANZAHL_EINGABEFELDER
Me.Controls("TextBox" & i) = ""
Next i
ListBox1.Clear
ListBox1.ColumnCount = 3
ListBox1.ColumnWidths = "0;75;;"
lZeileMaximum = Tabelle5.UsedRange.Rows.Count
For lZeile = lCONST_STARTZEILENNUMMER_DER_TABELLE To lZeileMaximum
If IST_ZEILE_LEER(lZeile) = False Then
ListBox1.AddItem lZeile
ListBox1.List(ListBox1.ListCount - 1, 1) = CStr(Tabelle5.Cells(lZeile, 1).Text)
ListBox1.List(ListBox1.ListCount - 1, 2) = CStr(Tabelle5.Cells(lZeile, 2).Text)
ListBox1.List(ListBox1.ListCount - 1, 3) = CStr(Tabelle5.Cells(lZeile, 3).Text)
End If
Next lZeile
End Sub
Private Sub EINTRAG_LADEN_UND_ANZEIGEN()
Dim lZeile As Long
Dim i As Integer
For i = 1 To iCONST_ANZAHL_EINGABEFELDER
Me.Controls("TextBox" & i) = ""
Next i
If ListBox1.ListIndex >= 0 Then
lZeile = ListBox1.List(ListBox1.ListIndex, 0)
For i = 1 To iCONST_ANZAHL_EINGABEFELDER
Me.Controls("TextBox" & i) = CStr(Tabelle5.Cells(lZeile, i).Text)
Next i
End If
End Sub
Private Sub EINTRAG_SPEICHERN()
Dim lZeile As Long
Dim i As Integer
If ListBox1.ListIndex = -1 Then Exit Sub
lZeile = ListBox1.List(ListBox1.ListIndex, 0)
For i = 1 To iCONST_ANZAHL_EINGABEFELDER
Tabelle5.Cells(lZeile, i) = Me.Controls("TextBox" & i)
Next i
ListBox1.List(ListBox1.ListIndex, 1) = TextBox1
ListBox1.List(ListBox1.ListIndex, 2) = TextBox2
ListBox1.List(ListBox1.ListIndex, 3) = TextBox3
End Sub
Private Sub EINTRAG_LOESCHEN()
Dim lZeile As Long
If ListBox1.ListIndex = -1 Then Exit Sub
If MsgBox("Sie möchten den markierten Datensatz wirklich löschen?", _
vbQuestion + vbYesNo, "Sicherheitsabfrage!") = vbYes Then
lZeile = ListBox1.List(ListBox1.ListIndex, 0)
Tabelle5.Rows(CStr(lZeile & ":" & lZeile)).Delete
ListBox1.RemoveItem ListBox1.ListIndex
End If
End Sub
Private Sub EINTRAG_ANLEGEN()
Dim lZeile As Long
lZeile = lCONST_STARTZEILENNUMMER_DER_TABELLE
Do While IST_ZEILE_LEER(lZeile) = False
lZeile = lZeile + 1
Loop
Tabelle5.Cells(lZeile, 1) = CStr("Neuer Eintrag Zeile " & lZeile)
ListBox1.AddItem lZeile
ListBox1.List(ListBox1.ListCount - 1, 1) = CStr("Neuer Eintrag Zeile " & lZeile)
ListBox1.List(ListBox1.ListCount - 1, 2) = ""
ListBox1.List(ListBox1.ListCount - 1, 3) = ""
ListBox1.ListIndex = ListBox1.ListCount - 1
TextBox1.SetFocus
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1)
End Sub
' HILFSFUNKTIONEN
Private Function IST_ZEILE_LEER(ByVal lZeile As Long) As Boolean
Dim i As Long
Dim sTemp As String
sTemp = ""
For i = 1 To iCONST_ANZAHL_EINGABEFELDER
sTemp = sTemp & Trim(CStr(Tabelle5.Cells(lZeile, i).Text))
Next i
If Trim(sTemp) = "" Then
'Die Zeile ist leer
IST_ZEILE_LEER = True
Else
IST_ZEILE_LEER = False
End If
End Function