time clock with button

ocular

New Member
Joined
Jan 22, 2025
Messages
0
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Sub LogTimeSimple()
Dim ws As Worksheet
Dim currentTime As String
Dim lastRow As Long
Dim currentCell As Range

' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")

' Get the current time and date
currentTime = Format(Time, "HH:mm:ss")

' Find the last row with a date in column A
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

' If the last row is empty or not today, start a new row
If ws.Cells(lastRow, 1).Value <> Format(Date, "MM/DD/YYYY") Then
lastRow = lastRow + 1
ws.Cells(lastRow, 1).Value = Format(Date, "MM/DD/YYYY")
End If

' Loop through columns B to E for the current row
For Each currentCell In ws.Range(ws.Cells(lastRow, 2), ws.Cells(lastRow, 5))
If currentCell.Value = "" Then
currentCell.Value = currentTime
Exit Sub
End If
Next currentCell
End Sub
 
Welcome to the MrExcel board!

Looks like you are testing how to post vba code in the forum. It is best to use the available code tags. It makes your code much easier to read/debug & copy. My signature block at the bottom of this post has more details. Here is your code again with the standard vba tags applied. 😊

VBA Code:
Sub LogTimeSimple()
    Dim ws As Worksheet
    Dim currentTime As String
    Dim lastRow As Long
    Dim currentCell As Range
   
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
   
    ' Get the current time and date
    currentTime = Format(Time, "HH:mm:ss")
   
    ' Find the last row with a date in column A
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
   
    ' If the last row is empty or not today, start a new row
    If ws.Cells(lastRow, 1).Value <> Format(Date, "MM/DD/YYYY") Then
        lastRow = lastRow + 1
        ws.Cells(lastRow, 1).Value = Format(Date, "MM/DD/YYYY")
    End If
   
    ' Loop through columns B to E for the current row
    For Each currentCell In ws.Range(ws.Cells(lastRow, 2), ws.Cells(lastRow, 5))
        If currentCell.Value = "" Then
            currentCell.Value = currentTime
            Exit Sub
        End If
    Next currentCell
End Sub
 

Forum statistics

Threads
1,226,059
Messages
6,188,634
Members
453,487
Latest member
LZ_Code

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top