PureBluff
Board Regular
- Joined
- Apr 4, 2014
- Messages
- 174
- Office Version
- 2016
- Platform
- Windows
- Mobile
Hi all,
It's been almost a year since I wrote some VBA after some health issues and I'm struggling a bit so looking for a steer.
I need to write a clocking in/out "timesheet" - measuring a barcode scan as an in/out movement of an area.
Column A is a barcode scan (UserID)
Column B is the time in to the area )-- 1st / odd scans
Column C is the time out -- 2nd / even scans
My end goal was the user scans and Excel works out if it's an in or out based on a value being in a cell or not
e.g.
On a worksheet_change event I wish to search column A, find the UserID (if it exists)
- if it exists, check for a clock out scan, if its clocked out, findnext / loop until it finds a match where there isnt a clock out
`- once found add the clockout scan
- if it doesnt exist, or, if theres no scans without an out-scan (so a user is scanning back in the zone) - add the record and timestamp (format(now(),"hh:mm")
I've tried a loop while x is not nothing and gotten myself in a mess where I can't meet all the variables and can only thing I'm approaching it wrong and there must be a simpler way, hence my plea for help.
I'll put my code below but I am under no illusion its poor but I was trying to build it over time but as above, can't get it to hit the right variables and stay in the loop and I'm too confused now.
It's been almost a year since I wrote some VBA after some health issues and I'm struggling a bit so looking for a steer.
I need to write a clocking in/out "timesheet" - measuring a barcode scan as an in/out movement of an area.
Column A is a barcode scan (UserID)
Column B is the time in to the area )-- 1st / odd scans
Column C is the time out -- 2nd / even scans
My end goal was the user scans and Excel works out if it's an in or out based on a value being in a cell or not
e.g.
On a worksheet_change event I wish to search column A, find the UserID (if it exists)
- if it exists, check for a clock out scan, if its clocked out, findnext / loop until it finds a match where there isnt a clock out
`- once found add the clockout scan
- if it doesnt exist, or, if theres no scans without an out-scan (so a user is scanning back in the zone) - add the record and timestamp (format(now(),"hh:mm")
I've tried a loop while x is not nothing and gotten myself in a mess where I can't meet all the variables and can only thing I'm approaching it wrong and there must be a simpler way, hence my plea for help.
I'll put my code below but I am under no illusion its poor but I was trying to build it over time but as above, can't get it to hit the right variables and stay in the loop and I'm too confused now.
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
LastRow = Sheets("Loop1").Cells(Rows.Count, 1).End(xlUp).Row - 1
If Target.Column = 1 Then
ThisRow = Target.Row
If Target.Value <> "" Then
Dim c As Range
With Worksheets("Loop1").Range("A2:A" & LastRow)
Set c = .Find(Target.Value, LookIn:=xlValues)
If Not c Is Nothing And Range(c.Address).Offset(0, 2).Value <> "" Then
Do
Set c = .FindNext(c)
Loop While Not c Is Nothing And Range(c.Address).Offset(0, 2).Value <> ""
Else
Do
ActiveSheet.Unprotect Password:="t3st"
Range(c.Address).Offset(0, 2).Value = Format(Now(), "hh:mm")
Range(Target.Address).Value = ""
End
Loop While Not c Is Nothing
End If
End With
ActiveSheet.Unprotect Password:="t3st"
Range("B" & ThisRow).Value = Format(Now(), "hh:mm")
ActiveSheet.Protect Password:="t3st"
Else
Exit Sub
End If
End If
End Sub