Private Sub Button1_Click()
Dim userID As String
Dim password As String
Dim punchType As String
' ユーザー認証
userID = InputBox("ユーザーIDを入力してください:")
password = InputBox("パスワードを入力してください:")
If Not Authenticate(userID, password) Then
MsgBox "認証に失敗しました。"
Exit Sub
End If
' 打刻タイプの選択
punchType = InputBox("打刻タイプを入力してください: (出勤, 退勤, 休憩開始, 休憩終了)")
' 打刻処理
PunchClock userID, punchType
MsgBox punchType & "打刻が完了しました。"
End Sub
Function Authenticate(userID As String, password As String) As Boolean
Dim ws As Worksheet
Dim found As Range
Set ws = ThisWorkbook.Sheets("Users")
Set found = ws.Columns("A").Find(What:=userID, LookIn:=xlValues, LookAt:=xlWhole)
If Not found Is Nothing Then
If ws.Cells(found.Row, 2).Value = password Then
MsgBox "ようこそ、" & userID & "さん"
Authenticate = True
Else
MsgBox "パスワードが間違っています。"
Authenticate = False
End If
Else
MsgBox "ユーザーIDが見つかりません。"
Authenticate = False
End If
End Function
Sub PunchClock(userID As String, punchType As String)
Dim ws As Worksheet
Dim currentTime As String
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Users")
currentTime = Format(Now, "yyyy-mm-dd hh:mm:ss")
' データの記録
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
ws.Cells(lastRow, 1).Value = userID
ws.Cells(lastRow, 3).Value = punchType
ws.Cells(lastRow, 4).Value = currentTime
End Sub