Worksheet_change | Find target.value / loop through results with multiple actions

PureBluff

Board Regular
Joined
Apr 4, 2014
Messages
174
Office Version
  1. 2016
Platform
  1. Windows
  2. 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.

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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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