How to Use VBA Find to Update Stock From Another Excel Workbook
Question details
The user needs a reliable VBA macro to look up an SKU in an external stock workbook, deduct the invoiced quantity from the adjacent cell, and save the file without using unstable selection methods.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Managing business inventory by automatically deducting stock quantities based on SKUs listed in a separate invoice file.
- Observed behavior
- Needs a method to accurately find an SKU in a different workbook and update the stock cell directly, avoiding common object errors caused by Select, Selection, ActiveCell, and unnecessary Offset methods.
Ensure you have created backup copies of both your invoice workbook and stock database before running any VBA macros that automatically modify and save data. You must also enable macros in your spreadsheet security settings to allow the code to run.
Use the VBA Find Method to Update the Target Workbook Directly
This approach reliably opens the stock workbook, searches for the SKU, updates the adjacent quantity, and saves the file without using unstable Select or ActiveCell references.
By declaring workbook and worksheet variables, you can reference the exact ranges directly. This bypasses the need to physically select cells, which improves macro execution speed and prevents runtime errors if the user clicks away during the process.
Open the VBA Editor (Alt + F11) and declare your variables using `Dim wbStock As Workbook`, `Dim wsStock As Worksheet`, and a `Range` variable for your Find result.
Use `Set wbStock = Workbooks.Open("C:\Path\To\Your\StockFile.xlsx")` to open the external database file and define the specific worksheet to search.
Set up a `Do While` loop on your invoice sheet that reads each SKU row by row, and configure the loop to stop when it encounters an empty SKU cell.
Inside the loop, use `Set foundCell = wsStock.Columns("A").Find(What:=skuValue, LookIn:=xlValues, LookAt:=xlWhole)` to locate the matching SKU in the stock worksheet.
Add an `If Not foundCell Is Nothing Then` statement. Inside this block, directly subtract the invoice quantity from the adjacent cell using `foundCell.Offset(0, 1).Value = foundCell.Offset(0, 1).Value - invoiceQty`.
After the loop finishes processing all SKUs, save and close the stock database using `wbStock.Close SaveChanges:=True`.

Combine Invoice and Stock Data into a Single Workbook
If writing cross-workbook VBA seems too complex or unstable, merging your data into one workbook allows you to use standard lookup formulas and much simpler macros.
Easily Run VBA Macros to Manage Inventory with WPS Office
WPS Spreadsheet offers excellent compatibility with Microsoft Excel VBA macros. You can effortlessly run your stock-updating VBA scripts, use advanced formulas, and manage your business inventory in a lightweight and familiar interface.
- 1. Open Macro-Enabled Files: Launch WPS Spreadsheet and open your invoice and stock files (.xlsm format).
- 2. Access the VBA Editor: Navigate to the Developer tab on the top ribbon and click 'Macros' or 'Visual Basic' to open the built-in VBA editor.
- 3. Paste and Edit Your Code: Insert a new module and paste your VBA Find and Update script. You can edit and debug directly within the WPS VBA environment.
- 4. Run the Macro: Execute the macro to automatically process your invoices and accurately update your stock numbers across workbooks.

Frequently Asked Questions
Why shouldn't I use Select or ActiveCell in my VBA macro?
Using Select or ActiveCell forces the macro to physically mimic a user's clicks, which slows down execution significantly. It can also cause the macro to crash or overwrite the wrong data if another window gains focus or if the user clicks elsewhere during the process. Direct object referencing is much safer.
What happens if the VBA Find method cannot find the requested SKU?
If the Find method does not locate the target SKU, it returns an empty object (Nothing). If you attempt to update an offset of this empty object, your code will crash with an 'Object variable or With block variable not set' error. Always use 'If Not cell Is Nothing Then' to verify the match before modifying data.
Can I run this VBA macro without visibly opening the stock workbook?
Yes. You can add 'Application.ScreenUpdating = False' at the very beginning of your macro. This prevents Excel from visually loading the second workbook, making the process faster and cleaner. Remember to set it back to 'True' at the end of your script.
How do I process an invoice with multiple items using VBA?
You can use a 'Do While' or 'For Each' loop to iterate through the rows of your invoice. The loop should be configured to process each SKU row by row, update the external workbook, and automatically stop when it encounters an empty SKU cell.




