AW: Userform befüllen
26.12.2017 21:08:35
Piet
Hallo Silke
ich schicke dir hier wie gewünscht den geaenderten Code ohne Beispieltabelle. Einfach kopieren.
Im Modul1 musst du für die Ziel-Tabelle natürlich bei Public Const deinen Dateinamen einsetzen!
Die vielleicht etwas komplizierten "On Error" Befehle gehören unbedingt mit hinein!
Der Button zum Starten von UserForm2 befindet sich jetzt in der UserForm Mapppe!!
Danach sollte es problemlos laufen. Würde mich freuen wenn es klappt.
Dann haben wir uns beide ein frohes neues Jahr verdient!! ... - Prost
mfg Piet
Option Explicit 'Modul1:
'Öffentliche Variablen für Modul: UserForm2 !!
'** diese Variablen dürfen nicht gelöscht werden !!
'** hier bitte den eigenen Original Namen angeben, (unten ist meine Datei)
'** Die Quell Mappe = ThisWorkbbok, muss hier nicht extra benannt werden!!
Public Const Ziel_Mappe = "118511 Mitarbeiter füllen" 'Name der Ziel Mappe
Sub UF2_zeigen()
UserForm2.Show
End Sub
Option Explicit '26.12.2017 Piet Herber Forum
Dim AC As Range, sp As Integer, Datum As Date
Dim ZielWb As Object, ZTb As String
'Aenderungen buchen und Speichern
Private Sub CommandButton1_Click()
On Error GoTo 0
Datum = CDate(UserForm2.TextBox11)
ZTb = CStr(Right(UserForm2.TextBox11, 4))
If InStr(ZTb, ".") Then
MsgBox "'" & ZTb & "' - Das Jahr stimmt nicht - Eingabe nicht verbucht!!": Exit Sub
End If
On Error Resume Next 'Abbruch bei Fehler!!
'Zielmappe setzen - vier Stellen Jahresdatum
Set ZielWb = Workbooks(Ziel_Mappe).Worksheets(ZTb)
If Err > 0 Then MsgBox ZTb & " - Diese Jahres Tabelle exisitert nicht !!": Exit Sub
On Error GoTo Fehler 'Abbruch bei Fehler!!
'Suche nach Datum von B1-N1 (Find klappt nicht!!)
For Each AC In ZielWb.Range("B1:ND1")
If AC.Value = Datum Then sp = AC.Column: Exit For
Next AC
If sp = Empty Then MsgBox Datum & " kann Datum nicht finden!": Exit Sub
With UserForm2
ZielWb.Cells(3, sp) = .TextBox1.Value 'TextBox1 = Daten1
ZielWb.Cells(4, sp) = .TextBox7.Value 'TextBox7 = Daten2
ZielWb.Cells(6, sp) = .TextBox9.Value 'TextBox9 = Daten4
ZielWb.Cells(8, sp) = .TextBox5.Value 'TextBox5 = Daten6
ZielWb.Cells(11, sp) = .TextBox10.Value 'TextBox10 = Daten9
ZielWb.Cells(12, sp) = .TextBox4.Value 'TextBox4 = Daten10
ZielWb.Cells(13, sp) = .TextBox8.Value 'TextBox8 = Daten11
ZielWb.Cells(24, sp) = .TextBox3.Value 'TextBox3 = Daten22
ZielWb.Cells(27, sp) = .TextBox6.Value 'TextBox6 = Daten25
ZielWb.Cells(19, sp) = .TextBox2.Value 'TextBox2 = ** Labe1 =17
End With
'** speichert sofort nach Eingabe
Workbooks(Ziel_Mappe).Save
Exit Sub
Fehler: MsgBox Datum & " - unerwarteter Fehler beim Eingabe buchen!!"
End Sub
'UserForm2 aus 2017 laden
Private Sub TextBox11_AfterUpdate()
On Error GoTo 0
Datum = CDate(UserForm2.TextBox11)
ZTb = CStr(Right(UserForm2.TextBox11, 4))
If InStr(ZTb, ".") Then
MsgBox "'" & ZTb & "' - Das Jahr stimmt nicht - kann nicht befüllen!!": Exit Sub
End If
On Error Resume Next 'Abbruch bei Fehler!!
'Zielmappe setzen - vier Stellen Jahresdatum
Set ZielWb = Workbooks(Ziel_Mappe).Worksheets(ZTb)
If Err > 0 Then MsgBox ZTb & " - Diese Jahres Tabelle exisitert nicht !!": Exit Sub
On Error GoTo Fehler 'Abbruch bei Fehler!!
'Suche nach Datum von B1-N1 (Find klappt nicht!!)
For Each AC In ZielWb.Range("B1:ND1")
If AC.Value = Datum Then sp = AC.Column: Exit For
Next AC
If sp = Empty Then MsgBox Datum & " kann Datum nicht finden!": Exit Sub
With UserForm2
.TextBox1.Value = ZielWb.Cells(3, sp) 'TextBox1 = Daten1
.TextBox7.Value = ZielWb.Cells(4, sp) 'TextBox7 = Daten2
.TextBox9.Value = ZielWb.Cells(6, sp) 'TextBox9 = Daten4
.TextBox5.Value = ZielWb.Cells(8, sp) 'TextBox5 = Daten6
.TextBox10.Value = ZielWb.Cells(11, sp) 'TextBox10 = Daten9
.TextBox4.Value = ZielWb.Cells(12, sp) 'TextBox4 = Daten10
.TextBox8.Value = ZielWb.Cells(13, sp) 'TextBox8 = Daten11
.TextBox3.Value = ZielWb.Cells(24, sp) 'TextBox3 = Daten22
.TextBox6.Value = ZielWb.Cells(27, sp) 'TextBox6 = Daten25
.TextBox2.Value = ZielWb.Cells(19, sp) 'TextBox2 = ** Labe1 =17
End With
Exit Sub
Fehler: MsgBox Datum & " - unerwarteter Fehler beim TextBox befüllen!!"
End Sub