VBA & Macro Problems

How to Create a VBA Macro to Update Excel Fields for a Range of Lot Numbers

John WilsonJohn Wilson Nov 11, 2026 869 views

Question details

The user needs a VBA macro to find specific lot numbers within a range and automatically update the corresponding fields and formulas in other columns based on that data.

How to Create a VBA Macro to Update Excel Fields for a Range of Lot Numbers
Product
Excel
Device & OS
not provided
Scenario
Automating the update of formulas in columns F and P for rows where the lot number in D2:D60 matches a user-defined range, utilizing values from columns G and Q.
Observed behavior
The goal state is a macro that prompts the user for lot numbers, validates the input, safely iterates through the target range, updates the correct numeric fields, and displays a success message with the result.
Before you start

Before running any VBA macro that modifies multiple cells, always create a backup copy of your workbook, as changes made by macros cannot be undone using the standard Undo shortcut.

Solution 1Recommended

Implement a Custom VBA Macro to Filter and Update Lot Numbers

Use the built-in VBA Editor to create a script that prompts for starting and ending lot numbers, validates the input, and updates formulas only for matching rows.

This solution utilizes an InputBox to gather the target lot range from the user. It then loops through the specified range (D2:D60) to check for matches. If a match is found and the existing data is numeric, it dynamically calculates and applies the new formulas in the respective offset columns.

To ensure stability, the script includes error handling for invalid input (such as entering letters instead of numbers or inverted ranges) and uses appropriate data types like Integer and Double.

1
Open the VBA Editor

Open your workbook and press Alt + F11 to launch the Visual Basic for Applications (VBA) Editor. Ensure that the Developer tab is enabled in your ribbon if you prefer to access it via the menu.

2
Insert a New Module

In the Project Explorer pane on the left, right-click on your workbook's name. Hover over Insert and click on Module to create a blank canvas for your code.

3
Write the Macro Code

Define your Sub procedure (e.g., Sub AdjustFieldsBasedOnLots). Use Application.InputBox with Type:=1 to prompt for numeric start and end lots. Loop through YourWorksheet.Range("D2:D60"). Inside the loop, check if the cell value falls between your start and end lot variables.

4
Apply Offset Logic for Formulas

Use cell.Offset(0, X) to reference adjacent columns dynamically. Wrap your formula updates in an IsNumeric() check to prevent type mismatch errors on blank or text cells. After the loop, add a MsgBox to notify you that the fields were updated successfully.

5
Run the Macro

Close the VBA Editor. In your spreadsheet, press Alt + F8 to open the Macro dialog box, select your newly created macro, and click Run. Test it with a small range first to verify the results.

Implement a Custom VBA Macro to Filter and Update Lot Numbers
Best Practice: Always include 'Option Explicit' at the very top of your module. This forces you to declare all variables properly, preventing typos and reducing memory usage overhead.
Advanced Automation

Automate Data Updates with Macros in WPS Spreadsheet

WPS Spreadsheet provides comprehensive support for VBA macros, allowing you to easily build, test, and execute scripts that automate repetitive tasks like updating fields based on lot numbers.

  1. 1. Enable the Developer Tools: Open your file in WPS Spreadsheet, navigate to the Developer tab on the ribbon, and ensure macros are enabled in your security settings.
  2. 2. Access the VBA Editor: Click the VBA Editor button to open the coding environment. You can insert modules and paste your macro code directly.
  3. 3. Execute the Script: Use the Macros button in the Developer tab to select and run your lot number update script seamlessly.
Fully compatible with Microsoft Excel macro-enabled files (.xlsm)Built-in robust Developer tools and VBA EditorLightweight performance for processing loops over large datasetsFree to use with an intuitive, familiar user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA macro throwing a Type Mismatch error?

A Type Mismatch error usually occurs when the macro encounters text or blank cells while expecting numeric data. Ensure you wrap your calculation logic inside an 'If IsNumeric()' statement before updating the cell.

How can I adjust the VBA code if my lot numbers are in a different column?

You need to update the Range reference in the For Each loop. For example, change 'ws.Range("D2:D60")' to 'ws.Range("A2:A60")'. Keep in mind you will also need to adjust the column offsets (e.g., cell.Offset) to point to the correct columns relative to the new target.

Can I undo the changes made by this macro?

Standard Undo functionality (Ctrl + Z) does not work for actions performed by VBA macros. Always save a backup copy of your workbook before running any script that overwrites formulas or data.

How do I make the InputBox only accept numbers?

By using 'Application.InputBox' instead of the standard 'InputBox' function, you can specify 'Type:=1'. This forces Excel to only accept numeric values, automatically handling basic user input validation for your lot numbers.