VBA & Macro Problems

Use Excel VBA to Record Barcode Scans with Timestamps

Nimra MalikNimra Malik Oct 12, 2026 873 views

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.

Use Excel VBA to Record Barcode Scans with Timestamps
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Access the Worksheet Object

In the Project Explorer pane on the left, double-click the sheet named 'PICK VALIDATION' where the barcode scanning occurs.

3
Create the Worksheet_Change Subroutine

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.

4
Add the Target Verification and Disable Events

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.

5
Write Data to the Scanned History Sheet

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'.

6
Re-enable Application Events

Before ending the If block and the subroutine, add 'Application.EnableEvents = True' to ensure macros continue working for future scans.

Use Worksheet_Change Event to Log Scans and Timestamps
Exact Sheet Names Required: Ensure that a worksheet named exactly 'SCANNED HISTORY' exists in your workbook, otherwise the script will return an out-of-range error when a barcode is scanned.

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. 1. Open your workbook in WPS Spreadsheet: Launch WPS Office and open your inventory or scanning workbook.
  2. 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. 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. 4. Save and Run: Save your file as a Macro-Enabled Workbook (.xlsm) and start scanning barcodes to automatically generate your timestamped history log.
Highly compatible with Microsoft Excel VBA macros and .xlsm filesFree, lightweight, and efficient for handling large data logsBuilt-in Developer tab with a complete VB Editor for easy script creation and debugging
microsoft office alternative - wps office

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.