AW: Combobox
04.12.2008 18:25:13
fcs
Hallo Markus,
den Sinn dieser Combox-Variante versteh ich zwar nicht ganz, aber sei es drum.
In deiner aktuellen Version würde ich den Code für den OK-Button an deiner Stelle wie folgt anpassen:
Private Sub CommandButton2_Click()
Dim zeile As Long
If UserForm1.tb_nachname = "" Then
MsgBox "Eingabe unvollständig, Name fehlt!"
ElseIf UserForm1.tb_vorname = "" Then
MsgBox "Eingabe unvollständig, Vorname fehlt!"
ElseIf UserForm1.tb_straße = "" Then
MsgBox "Eingabe unvollständig, Strasse fehlt!"
ElseIf UserForm1.tb_plz = "" Then
MsgBox "Eingabe unvollständig, PLZ fehlt!"
ElseIf UserForm1.tb_ort = "" Then
MsgBox "Eingabe unvollständig, Ort fehlt!"
Else
With ActiveSheet
zeile = .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0).Row
.Cells(zeile, 1).Value = UserForm1.tb_vorname
.Cells(zeile, 2).Value = UserForm1.tb_nachname
.Cells(zeile, 3).Value = UserForm1.tb_straße
.Cells(zeile, 4).Value = UserForm1.tb_plz
.Cells(zeile, 5).Value = UserForm1.tb_ort
End With
UserForm1.tb_vorname = ""
UserForm1.tb_nachname = ""
UserForm1.tb_straße = ""
UserForm1.tb_plz = ""
UserForm1.tb_ort = ""
End If
End Sub
Für die Variante mit der Combox legts du im Formular eine Combobox mit Namen Combobox1 und ein Textfeld mit Namen tb_Eingabe. Dann den folgenden Code für OK-Button und die Initialisierungsprozedur des Userforms.
Private Sub CommandButton2_Click()
Dim zeile As Long, Spalte As Long
If Me.ComboBox1.ListIndex -1 Then
If UserForm1.tb_Eingabe = "" Then
MsgBox "Eingabe unvollständig, Eingabewert fehlt!"
Else
With ActiveSheet
Spalte = Me.ComboBox1.ListIndex + 1
zeile = .Cells(.Rows.Count, Spalte).End(xlUp).Offset(1, 0).Row
.Cells(zeile, Spalte).Value = UserForm1.tb_Eingabe
End With
UserForm1.tb_Eingabe = ""
End If
Else
MsgBox "Bitte in Combobox eine Spalte auswählen!"
End If
End Sub
Private Sub UserForm_Initialize()
Dim wks As Worksheet, Spalte As Long
Set wks = ActiveSheet
'Spaltentitel aus Zeile 1 einlesen in Combobox1
With wks
Me.ComboBox1.Clear
For Spalte = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column
Me.ComboBox1.AddItem .Cells(1, Spalte).Text
Next
End With
End Sub
Viel Glück
Franz