Private Sub CommandButton1_Click()
Dim sn As String
Dim wsLog As Worksheet
Dim lastRow As Long
Dim currentTime As String
Set wsLog = ThisWorkbook.Sheets("Log")
sn = wsLog.Range("C1").Value ' Scan input cell
currentTime = Format(Now, "mm/dd/yyyy hh:mm:ss AM/PM")
If sn = "" Then
MsgBox "Please scan a barcode."
Exit Sub
End If
' Check if item was already scanned out
Dim foundRow As Long
Dim i As Long
foundRow = 0
For i = wsLog.Cells(wsLog.Rows.Count, "B").End(xlUp).Row To 3 Step -1
If wsLog.Cells(i, 2).Value = sn Then
foundRow = i
Exit For
End If
Next i
If foundRow > 0 And wsLog.Cells(foundRow, 4).Value = "" Then
' Log Check-In
wsLog.Cells(foundRow, 4).Value = currentTime
wsLog.Cells(foundRow, 5).Formula = "=D" & foundRow & "-C" & foundRow
MsgBox "Checked IN successfully!"
Else
' Log Check-Out
lastRow = wsLog.Cells(wsLog.Rows.Count, "B").End(xlUp).Row + 1
wsLog.Cells(lastRow, 2).Value = sn
wsLog.Cells(lastRow, 3).Value = currentTime
MsgBox "Checked OUT successfully!"
End If
wsLog.Range("C1").Value = "" ' Clear scan box
End Sub
Here's what I used so far now I changed B2 to C1 to put it in the space I wished
Dim sn As String
Dim wsLog As Worksheet
Dim lastRow As Long
Dim currentTime As String
Set wsLog = ThisWorkbook.Sheets("Log")
sn = wsLog.Range("C1").Value ' Scan input cell
currentTime = Format(Now, "mm/dd/yyyy hh:mm:ss AM/PM")
If sn = "" Then
MsgBox "Please scan a barcode."
Exit Sub
End If
' Check if item was already scanned out
Dim foundRow As Long
Dim i As Long
foundRow = 0
For i = wsLog.Cells(wsLog.Rows.Count, "B").End(xlUp).Row To 3 Step -1
If wsLog.Cells(i, 2).Value = sn Then
foundRow = i
Exit For
End If
Next i
If foundRow > 0 And wsLog.Cells(foundRow, 4).Value = "" Then
' Log Check-In
wsLog.Cells(foundRow, 4).Value = currentTime
wsLog.Cells(foundRow, 5).Formula = "=D" & foundRow & "-C" & foundRow
MsgBox "Checked IN successfully!"
Else
' Log Check-Out
lastRow = wsLog.Cells(wsLog.Rows.Count, "B").End(xlUp).Row + 1
wsLog.Cells(lastRow, 2).Value = sn
wsLog.Cells(lastRow, 3).Value = currentTime
MsgBox "Checked OUT successfully!"
End If
wsLog.Range("C1").Value = "" ' Clear scan box
End Sub
Here's what I used so far now I changed B2 to C1 to put it in the space I wished