How to Fix Excel VBA Barcode Lookup and Runtime Error 9
Question details
The user needs to configure an Excel VBA user form to search for scanned barcodes on a different worksheet, and needs to resolve a Runtime Error 9 that appeared after deleting a practice sheet.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Building a barcode scanning system with an Excel user form that retrieves matching data (like names and expiration dates) from a separate worksheet.
- Observed behavior
- The VBA code fails to process the barcode data and triggers a 'Subscript out of range' (Runtime Error 9) because a referenced worksheet was removed from the workbook.
Before troubleshooting, open the Visual Basic Editor (Alt + F11) and compile your VBA project to pinpoint the exact line of code triggering the error. Verify the exact spelling of all worksheet tabs in your active workbook to ensure they match your code.
Resolve VBA Runtime Error 9 (Subscript Out of Range)
Fix the error caused by referencing a deleted, renamed, or misspelled worksheet in your VBA code.
Runtime Error 9 typically occurs when your code asks Excel to interact with a specific object, like a Worksheet or Workbook, that cannot be found. If you recently deleted a practice sheet, any code pointing to it will break.
Run your macro or user form to trigger the error, then click 'Debug' on the error prompt. This will open the Visual Basic Editor and highlight the failing line in yellow.
Check the highlighted line for any Sheets("SheetName") or Workbooks("WorkbookName") references. Compare these names against the actual tabs visible at the bottom of your Excel window.
Update the referenced string in the code to exactly match an existing worksheet tab. If the deleted sheet (e.g., 'Sheet3') is no longer needed, remove the line of code referencing it entirely.

Implement VBA Find for Barcode Lookup
Use the VBA Find method to search for a scanned barcode on another worksheet and retrieve its matching data.
Run VBA Barcode Scanners and Macros Seamlessly in WPS Office
WPS Spreadsheet provides robust support for VBA and macros, allowing you to run your barcode lookup scripts and user forms without compatibility issues. You can easily manage, debug, and execute your existing VBA projects.
- 1. Enable VBA support: Download and install WPS Office, ensuring you select a version that includes VBA macro support for spreadsheets.
- 2. Open your workbook: Open your existing .xlsm or .xlsb barcode scanner workbook directly in WPS Spreadsheet.
- 3. Access the Developer tab: Navigate to the Developer tab on the top ribbon and click 'Visual Basic' to open the VBA Editor.
- 4. Run and debug: Test your barcode lookup user form. If any subscript errors occur, use the built-in WPS debugger to update missing worksheet references quickly.

Frequently Asked Questions
What does Runtime Error 9 'Subscript out of range' mean in VBA?
This error occurs when your VBA code attempts to access an item in a collection that does not exist. In Excel, this almost always happens if you reference a worksheet, workbook, or array index that has been deleted, renamed, or spelled incorrectly.
How do I keep my Excel VBA user form in front of other windows?
You can make a user form 'modeless' so it stays on top of the application while still allowing you to interact with the underlying spreadsheet. To do this, change the code that loads the form to UserForm1.Show vbModeless, or change the ShowModal property to False in the VBA Properties window.
Why is my VBA Find method failing to locate the scanned barcode?
The VBA Find method might fail if the barcode formatting doesn't exactly match the search criteria. Ensure you are searching the correct worksheet range, set LookIn:=xlValues to look at the actual data rather than formulas, and verify that LookAt:=xlWhole is used so it doesn't match partial strings by mistake.




