VBA - Create Punch Time

poupinhas

New Member
Joined
Apr 11, 2023
Messages
2
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
Hi everyone. I'm new to the website and also new to using VBA in Excel.
I'm trying to create a punch time table for workers to clock in and out.
I want do add a button that each click inserts the current time in the first empty cell it encounters from left to right and from top to bottom
It starts at C13 and ends at F43
It should insert clock in time in C13 than clock out in D13 than clock in again at E13 and clock out again at F13. And than it should jump to C14 than D14, E14, F14 and so on.
Can anyone help me?
I was using a macro that filled the time in line but it didn't jump to the next days.

Thanks
 

Attachments

  • 2023-04-11 16_36_26-Pica - Excel.png
    2023-04-11 16_36_26-Pica - Excel.png
    23.8 KB · Views: 21

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this on a copy of your sheet. You will need to change 004 to your sheet name. See the note I wrote about assuming that in any row with time data, C will not be blank (so no value in C means no value in D,E or F either. You may need to turn off events if this code causes other events to run and you don't want that to happen.
VBA Code:
Sub FindEmptyInRange()
Dim ws As Worksheet, rng As Range
Dim Lrow As Long

Set ws = Sheets("004")
'not guaranteed but asssume last row will have a value in column A
Lrow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Set rng = ws.Cells.Find("", After:=Range("C" & Lrow), LookAt:=xlPart, LookIn:=xlFormulas, _
           SearchOrder:=xlByRows, SearchDirection:=xlNext)
If rng.Column <= 7 And rng.Column >= 3 Then
    rng = Time
Else
    ws.Range("C" & rng.Row + 1) = Time
End If

Set rng = Nothing
Set ws = Nothing

End Sub
 
Upvote 0
Solution
It works like a
Try this on a copy of your sheet. You will need to change 004 to your sheet name. See the note I wrote about assuming that in any row with time data, C will not be blank (so no value in C means no value in D,E or F either. You may need to turn off events if this code causes other events to run and you don't want that to happen.
VBA Code:
Sub FindEmptyInRange()
Dim ws As Worksheet, rng As Range
Dim Lrow As Long

Set ws = Sheets("004")
'not guaranteed but asssume last row will have a value in column A
Lrow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
Set rng = ws.Cells.Find("", After:=Range("C" & Lrow), LookAt:=xlPart, LookIn:=xlFormulas, _
           SearchOrder:=xlByRows, SearchDirection:=xlNext)
If rng.Column <= 7 And rng.Column >= 3 Then
    rng = Time
Else
    ws.Range("C" & rng.Row + 1) = Time
End If

Set rng = Nothing
Set ws = Nothing

End Sub
It works like a charm. Thank you very much :D
 
Upvote 0
You're welcome. Maybe mark this one as solved to save others time when looking for threads to solve?
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,442
Members
448,898
Latest member
drewmorgan128

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