Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Sub TimeRecording()
Dim lngMaxRow As Long
Dim lngMaxCol As Long
Dim lngRow As Long
Dim lngCol As Long
Dim lngCount As Long
Dim lngResult As Long
Dim strUserName As String
Dim intPos As Integer
Dim strToday As String
Dim strTime As String
Dim objSheet As Worksheet
Dim objRang As Range
' ログオンユーザー取得
strUserName = Space$(256)
lngResult = GetUserName(strUserName, Len(strUserName))
intPos = InStr(strUserName, vbNullChar)
If intPos > 0 Then
strUserName = Left$(strUserName, intPos - 1)
End If
' 日時取得
strToday = Format(Now, "yyyy/mm/dd")
strTime = Format(Now, "hh:mm:ss")
Set objSheet = ActiveSheet
With objSheet.Cells.SpecialCells(xlCellTypeLastCell)
lngMaxRow = .Row
lngMaxCol = .Column
End With
' 1行目の場合、見出しをセット
If lngMaxRow = 1 Then
objSheet.Cells(1, 1).Value = "日付"
objSheet.Cells(1, 2).Value = strUserName
lngCol = 2
Else
Set objRang = objSheet.Cells.Find(strUserName)
If objRang Is Nothing Then
lngCol = lngMaxCol + 1
objSheet.Cells(1, lngCol).Value = strUserName
Else
If objRang.Row = 1 Then
lngCol = objRang.Column
Else
lngCol = lngMaxCol + 1
objSheet.Cells(1, lngCol).Value = strUserName
End If
End If
End If
' 日付をセット
Set objRang = objSheet.Cells.Find(strToday)
If objRang Is Nothing Then
lngRow = lngMaxRow + 1
With objSheet.Cells(lngRow, 1)
.NumberFormatLocal = "@"
.Value = strToday
End With
Else
If objRang.Column = 1 Then
lngRow = lngMaxRow
Else
lngRow = lngMaxRow + 1
objSheet.Cells(lngRow, 1).Value = strToday
End If
End If
' 時間をセット
Set objRang = objSheet.Cells(lngRow, lngCol)
If objRang = "" Then
objRang.Value = strTime
End If
End Sub