2

Good Day People, im kinda new to VBA.

Im trying to make a time and attandance. Im actually pretty far, but unforntuanly i ran into a problem.

I use 2 Userforms. One where you get with your id to your sheet and can then clock in or clock out on the userform2.

When they clocked in or clocked it, it goes back to the first form. Unfornuatly the form doesnt work then anymore. I tried loading, hide, show, exit sub, searched the internet but cant find a solution.

Maybe im doing something wrong, that the modul / Userform doesnt work anymore.

Im thankfull for any help.

Dont mind Names of Strings and stuff, its just a test before i do the real one.

Im using the modulobject for the code. Looks better for me So im refering in Userform1 Objects to a sub in the modullib.

Modul1:

Function DoesSheetExists(sh As String) As Boolean
    Dim ws As Worksheet

    On Error Resume Next
    Set ws = ThisWorkbook.Sheets(sh)
    On Error GoTo 0

    If Not ws Is Nothing Then DoesSheetExists = True
    
End Function

Sub ScanID()
Dim MitarbeiterWS As Worksheet
Dim MitarbeiterID As Range
Dim MitarbeiterIDstring As String
Dim MitarbeiterName As String

ThisWorkbook.Sheets("MitarbeiterID").Activate




MitarbeiterIDstring = UserForm1.TextBox1.Value


Set MitarbeiterID = Range("A1:A30").Find(What:=MitarbeiterIDstring)
If Not MitarbeiterID Is Nothing Then
    MitarbeiterName = MitarbeiterID.Offset(, 1).Value
    UserForm1.Label1 = MitarbeiterName

        If Not DoesSheetExists(MitarbeiterName) Then
            If Not MitarbeiterName = "" Then
            Sheets("Example").Copy After:=Sheets(Sheets.Count)
            ActiveSheet.Name = MitarbeiterName
        
           Unload UserForm1
            UserForm2.Show
            MitarbeiterID.Select
           
     
            
            
            Else
            End If
        Else
            ThisWorkbook.Sheets(MitarbeiterName).Activate
           Unload UserForm1
           UserForm2.Show
           
         
           
            
        End If

Else
      
  
End If
    


End Sub

Modul2

Sub Eincloggen()
Dim Einspalte As Range

Set Einspalte = Range("A1").End(xlDown)

'wenn zum ersten mal eingeclockt
If Einspalte.Offset(, 1).Value = "" Then
    If Einspalte.Value = "Clocked In" Then
    Einspalte.Offset(1, 0).Value = Now
    
    Else
    MsgBox "Erst Raus"
    
    End If
    
    
Else
Einspalte.Offset(1, 0).Value = Now

Unload UserForm2

UserForm2.Hide
UserForm1.Show


End If
End Sub

Sub Auscloggen()
Dim Einspalte As Range
Dim Ausspalte As Range
Dim Pausenspalte As Range
Dim Stundenspalte As Range


Set Einspalte = Range("A1").End(xlDown)
Set Ausspalte = Range("B1").End(xlDown)
Set Pausenspalte = Range("C1").End(xlDown)
Set Stundenspalte = Range("D1").End(xlDown)


If Ausspalte.Offset(1, -1).Value = "" Then

MsgBox "Erst Rein"
Else

Ausspalte.Offset(1, 0).Value = Now
If Not Ausspalte.Offset(1, 0).Value = "" Then

Stundenspalte.Offset(1, 0).Value = (Ausspalte.Offset(1, 0).Value) - (Einspalte.Value)


Stundenspalte.Offset(1, 0).NumberFormat = "hh:mm"

Unload UserForm2

UserForm2.Hide
UserForm1.Show





End If



End If

End Sub
Bennet S.
  • 21
  • 2
  • 1
    Looks like you went with [variant 1 here](https://stackoverflow.com/q/47288496/1188513); the answer there should help explain what's going on. – Mathieu Guindon Feb 05 '23 at 14:23
  • I tried your code and couldn't reproduce the error. How are you invoking the subs, by command buttons ? Does this need 2 forms ? I would have thought 1 form hiding and showing controls would suffice. The selection of person could be using a listbox or combobox. – CDP1802 Feb 07 '23 at 14:31
  • Even if i try simple good with smart Ui like below the new userform doesnt do anything. If i type in the textbox it wont change the label. UserForm1: Private Sub TextBox1_Change() Label1 = TextBox1.Value If Label1 = "Test" Then UserForm1.Hide UserForm2.Show End If End Sub UserForm2: Private Sub TextBox1_Change() Label1 = TextBox1.Value If Label1 = "Test" Then UserForm2.Hide UserForm1.Show End If End Sub – Bennet S. Feb 08 '23 at 20:52

0 Answers0