Use Excel VBA to Record Barcode Scans with Timestamps
Question details
The user wants to create an automated scan history by logging every barcode entered into a specific cell along with the current date and time on a separate worksheet.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Setting up a barcode scanning system where entering a value in cell K18 on the 'PICK VALIDATION' sheet triggers a macro to log the entry and a timestamp on the 'SCANNED HISTORY' sheet.
- Observed behavior
- The user needs a VBA script that correctly captures the barcode from the designated cell, locates the next available row in the history log, writes the data, and prevents infinite recursive loops triggered by the cell changes.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled macros in your spreadsheet security settings before writing and executing VBA code.
Use Worksheet_Change Event to Log Scans and Timestamps
Add an automated script to the sheet's Worksheet_Change event to copy the barcode value and current timestamp to a history log whenever cell K18 is updated.
By utilizing the Worksheet_Change event, Excel can automatically detect when a barcode scanner inputs a value into cell K18. To prevent the script from triggering itself recursively when it updates the history sheet, it is critical to disable application events before executing the main log writing procedure and re-enable them immediately afterward.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, double-click the sheet named 'PICK VALIDATION' where the barcode scanning occurs.
From the top dropdown menus in the code window, select 'Worksheet' on the left and 'Change' on the right. This generates the 'Private Sub Worksheet_Change(ByVal Target As Range)' block.
Add code to check if the changed cell is K18 (e.g., 'If Not Intersect(Target, Range("K18")) Is Nothing Then'). Inside this block, write 'Application.EnableEvents = False' to stop recursive triggers.
Reference the 'SCANNED HISTORY' sheet to find the next empty row (e.g., 'NextRow = Sheets("SCANNED HISTORY").Cells(Rows.Count, 1).End(xlUp).Row + 1'). Write the scanned value using 'Sheets("SCANNED HISTORY").Cells(NextRow, 1).Value = Target.Value' and the timestamp using 'Sheets("SCANNED HISTORY").Cells(NextRow, 2).Value = Now'.
Before ending the If block and the subroutine, add 'Application.EnableEvents = True' to ensure macros continue working for future scans.

Create Barcode Logs with Macros in WPS Spreadsheet
WPS Spreadsheet offers robust Macro and VBA compatibility, allowing you to seamlessly run automated scripts like barcode timestamp logging just like you would in Microsoft Excel.
- 1. Open your workbook in WPS Spreadsheet: Launch WPS Office and open your inventory or scanning workbook.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If it is not visible, enable it from the WPS options menu.
- 3. Open the VB Editor: Click on 'VB Editor' to launch the macro environment and double-click your specific sheet to paste the Worksheet_Change script.
- 4. Save and Run: Save your file as a Macro-Enabled Workbook (.xlsm) and start scanning barcodes to automatically generate your timestamped history log.

Frequently Asked Questions
Why does my VBA code loop endlessly or crash Excel when I update a cell?
This usually happens when you use a Worksheet_Change event that modifies cells. The modification triggers the event again, causing an infinite loop. To fix this, always include 'Application.EnableEvents = False' at the start of your cell-modifying code, and 'Application.EnableEvents = True' at the end.
How do I find the next empty row using VBA?
You can locate the next available row in a specific column by finding the last used row and adding 1. The standard syntax is: NextRow = Sheets("SheetName").Cells(Rows.Count, "A").End(xlUp).Row + 1.
Does WPS Office support running Excel VBA macros?
Yes, WPS Office Pro and specific versions provide excellent support for VBA macros. You can open .xlsm files, run existing VBA scripts, and use the built-in VB Editor to modify code seamlessly.
How can I format the 'Now' timestamp in VBA?
You can format the timestamp output by wrapping the 'Now' function in the Format function. For example, instead of just using 'Now', use 'Format(Now, "mm/dd/yyyy hh:mm:ss AM/PM")' to write the date and time in a specific layout.




