after being told to piece together a fraken-code sorta deal by a user on here I've come up with this:
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long
barcode = Worksheets("Sheet1").Cells(2, 2)
Set rng = Sheet1.Columns("a:a").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
ActiveSheet.Columns("a:a").Find("").Select
ActiveCell.Value = barcode
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
Else
rownumber = rng.Row
Worksheets("Sheet1").Cells(rownumber, 1).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
End If
ActiveCell.Offset(0, 3).Select
End Sub
All it does is take the barcode entry, put a time next to it, and enter it into the database. when you scan the code again it should put a time under in. my issue is; what command do I need to put in to delete the out timestamp once the in timestamp has been filled.
CodePudding user response:
Please review How to avoid using Select in Excel VBA - it will make your code more reliable
Without the Select/Activate:
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
barcode = ws.Cells(2, 2).Value
Set rng = ws.Columns("A").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
'checking out...
Set rng = ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = barcode
rng.Offset(0, 1).NumberFormat = "m/d/yyyy h:mm AM/PM"
rng.Offset(0, 1).Value = Date
Else
'checking in...
rng.Offset(0, 1).ClearContents
rng.Offset(0, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
rng.Offset(0, 2).Value = Date
End If
ws.Cells(2, 2) = ""
End Sub