AW: TextBox in UF in richtiges Format
fcs
Hallo Heinz,
da hatte ich scheinbar nicht alle Eingabevarianten durchgetestet. Mein Vorschlag reagiert noch allergisch aus leere Textboxen, da der Iff-Ausdruck komplett ausgewertet wird.
Wenn du nicht zwingend Daten eingeben willst, dann muss du die Werte in den Textboxen auf ="" prüfen und dann entweder die entsprechenden Aktionen bei Werteingabe überspringen bzw. den Inhalt in den zuordneten Zellen löschen.
Da du mehrere Textboxen in gleicher Weise Zellen ausfüllen lassen willst ist es sinnvoll hierfür eine Function zu erstellen, der "nur" jeweils Parameter übergeben werden.
Gruß
Franz
Private Sub CommandButton1_Click()
'Beispiel für Aufruf der Function für Zelle E96 und Textbox1
' - bei leerer Textbox erfolgt keine Aktion
If EingabeZahl(Zelle:=[E96], sTextboxname:="Textbox1") = False Then Exit Sub
'oder - bei leerer Textbox wird Null eingetragen
If EingabeZahl(Zelle:=[E96], sTextboxname:="Textbox1", bolNull:=True) = False Then Exit Sub
'oder - bei leerer Textbox wird Zellinhalt gelöscht, spezieller Meldetext bei Nicht-Zahl
If EingabeZahl(Zelle:=[E96], sTextboxname:="Textbox1", bolClear:=True, _
Msgtext:="Eingabe Beginn Montag muss Zahl sein!") = False Then Exit Sub
'oder - Eingabe in Textbox zwingend erforderlich
If EingabeZahl(Zelle:=[E96], sTextboxname:="Textbox1", bolMusseingabe:=True, _
Msgtext:="Eingabe Beginn Montag muss Zahl sein!") = False Then Exit Sub
End Sub
Private Function EingabeZahl(Zelle As Range, sTextboxname As String, _
Optional bolNull As Boolean = False, _
Optional bolClear As Boolean = False, _
Optional bolMusseingabe As Boolean = False, _
Optional Msgtext As String) As Boolean
Dim oControl As Control
Set oControl = Me.Controls(sTextboxname)
If oControl.Object.Value = "" Then
If bolMusseingabe = True Then
If Msgtext <> "" Then
MsgBox Msgtext, vbInformation + vbOKOnly, "Muss-Eingabe"
Else
MsgBox "Wert-Eingabe in """ & sTextboxname & """ ist erforderlich", _
vbInformation + vbOKOnly, "Muss-Eingabe"
End If
EingabeZahl = False
Else
If bolClear = True Then Zelle.ClearContents
If bolNull = True Then Zelle.Value = 0
End If
ElseIf IsNumeric(oControl.Object.Value) Then
Zelle.Value = Application.WorksheetFunction.Round(CDbl(oControl.Object.Value), 5)
Else
If Msgtext <> "" Then
MsgBox Msgtext, vbInformation + vbOKOnly, "Zahlenwerteingabe"
Else
MsgBox "Eingabe in """ & sTextboxname & """ ist keine Zahl", _
vbInformation + vbOKOnly, "Zahlenwerteingabe"
End If
EingabeZahl = False
End If
End Function