Hallo Niklas,
wo ist denn das Problem? Läuft's so nicht?
Kannst die Datei ja irgendwo uploaden und den Linnk hier posten.
Hier mal ein Beispiel für ne Klasse:
In ein Klassenmodul mit dem Namen "Datentransfer":
Option Explicit
Private Zellen() As Range
Private Controls() As Control
Public Bereich As Range
Public UF As UserForm
Public ControlType As String
Sub ZellenEinlesen(rng As Range)
Dim E1%, E2%
Set Bereich = rng
If Bereich Is Nothing Then Exit Sub
With Bereich
E1 = .Rows.Count
E2 = .Columns.Count
End With
ReDim Zellen(E1 * E2 - 1)
Dim r%, c%, i%
For r = 1 To E1
For c = 1 To E2
Set Zellen(i) = Bereich(r, c)
i = i + 1
Next
Next
End Sub
Sub ControlsEinlesen(ControlNames As String)
Dim Arr
Dim E1%, E2%, i%
If InStr(ControlNames, "-") Then
Arr = Split(ControlNames, "-")
If UBound(Arr) = 1 Then
If IsNumeric(Arr(0)) Then
E1 = Arr(0)
If IsNumeric(Arr(1)) Then
E2 = Arr(1)
Else: Exit Sub
End If
Else: Exit Sub
End If
ReDim Controls(E2 - E1)
Dim j%
For i = E1 To E2
Set Controls(j) = UF.Controls(ControlType & i)
j = j + 1
Next
End If
ElseIf InStr(ControlNames, ",") Then
Arr = Split(ControlNames, ",")
E2 = UBound(Arr)
ReDim Controls(E2)
For i = 0 To E2
Set Controls(i) = UF.Controls(ControlType & Arr(i))
Next
End If
If IsArray(Controls) Then
Dim x
For Each x In Controls
Debug.Print x.Name
Next
End If
End Sub
Sub Import()
Dim i%
For i = 0 To UBound(Controls)
Controls(i).Text = Zellen(i)
Next
End Sub
Sub Export()
Dim i%
For i = 0 To UBound(Controls)
Zellen(i) = Controls(i).Text
Next
End Sub
Function Überprüfen() As Boolean
Dim x
For Each x In Controls
If x.Text = "" Then
MsgBox "Bitte " & x.Name & " ausfüllen!"
Exit Function
End If
Next
Überprüfen = True
End Function
Sub SteuerelementeResetten()
Dim x As Control
For Each x In Controls
x.Text = ""
Next
End Sub
Und in das Modul der Userform:
Option Explicit
Dim DT As New Datentransfer
Private Sub UserForm_Initialize()
With DT
Set .UF = Me
.ZellenEinlesen Range("E28:G31")
.ControlType = "TextBox"
.ControlsEinlesen "2-11"
.Import
End With
End Sub
Private Sub UserForm_Activate()
'Erstes Auswahlfeld Markieren
With TextBox2
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
'Überschrift
Me.TextBox1.Value = ActiveSheet.Range("B36").Text
End Sub
Private Sub CommandButton1_Click()
With DT
.ControlsEinlesen "2-13"
If .Überprüfen Then
.ControlsEinlesen "2-12"
.Export
End If
End With
'UF_Tabelle_01.Hide 'hier eintragen
End Sub
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
CommandButton1.SetFocus
End Sub
Private Sub CommandButton2_Click()
UF_Tabelle_01.Hide 'hier eintragen
End Sub
Private Sub CommandButton2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
CommandButton2.SetFocus
End Sub
Private Sub CommandButton3_Click()
DT.Bereich = 0
Unload Me
UF_Tabelle_01.Show 'hier eintragen
End Sub
Private Sub CommandButton3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)
CommandButton3.SetFocus
End Sub
'check if value is numeric and stop if it isnt
Private Sub Check(str$, Key As MSForms.ReturnInteger)
If Not IsNumeric(str & Chr(Key)) Then
Key = 0
Beep
End If
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox5_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox7_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox8_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox9_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox10_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox11_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox12_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Private Sub TextBox13_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Check ActiveControl.Text, KeyAscii
End Sub
Insgesamt ist der Code so natürlich länger, macht bei einer einzelnen Userform die nicht zu umfangreich ist nicht so viel Sinn. Sobald du die Klasse und ihr Funktionen mehr als einmal benutzt aber schon, kann man auch noch erweitern...
Gruß
Till
|