How to Update Column B When Column A Equals a Specific Value Using VBA
Question details
The user needs to automatically populate a cell in Column B with a specific value only when the adjacent cell in Column A is changed to a designated target value, leaving Column B unchanged otherwise.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating data entry where a specific status or value in one column triggers an automatic update in an adjacent column.
- Observed behavior
- Currently, the user needs to manually update Column B, or use formulas that might overwrite existing values when the condition in Column A is not met.
Ensure your workbook is saved in a macro-enabled format (.xlsm) and that macros are enabled in your spreadsheet settings before adding VBA code.
Use a Worksheet_Change VBA Event
Implement a VBA macro to trigger automatically whenever Column A is modified, updating Column B only if the target value is met.
Formulas cannot preserve existing values conditionally without creating circular references. A VBA Worksheet_Change event is the recommended approach. This script checks if the changed cell is in Column A, temporarily disables events to prevent infinite loops, and updates the adjacent cell in Column B if the value matches your condition.
Right-click the worksheet tab at the bottom of your screen where you want this action to occur, and select 'View Code' from the context menu.
In the code module that appears, paste the following code: Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range If Not Intersect(Range("A:A"), Target) Is Nothing Then Application.ScreenUpdating = False Application.EnableEvents = False For Each rng In Intersect(Range("A:A"), Target) If rng.Value = "FOO" Then rng.Offset(0, 1).Value = "BAR" Next rng Application.EnableEvents = True Application.ScreenUpdating = True End If End Sub
Go to File > Save As, and change the file format to 'Excel Macro-Enabled Workbook (*.xlsm)'. Ensure macros are enabled when you reopen the file.

Automate Your Worksheets Seamlessly with WPS Office
WPS Office provides robust support for VBA and macros in its Spreadsheet application, allowing you to automate tasks like conditional column updates effortlessly.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your spreadsheet file.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic Editor' to manage your macros.
- 3. Run Your Macros: Paste your VBA code into the worksheet module, save the file as a macro-enabled workbook, and enjoy automated data entry.

Frequently Asked Questions
Can I achieve this using standard Excel formulas instead of VBA?
Standard formulas like IF statements can check Column A and update Column B, but they will overwrite existing data in Column B if the condition is false. To preserve existing data when the condition isn't met, VBA is required.
Why do I need to disable Application.EnableEvents in the VBA code?
Disabling Application.EnableEvents prevents the Worksheet_Change macro from triggering itself when it updates Column B. If left enabled, it could cause an infinite loop and crash your spreadsheet.
How do I modify the VBA code to check for multiple different values?
You can replace the single 'If' statement with a 'Select Case' block inside the For Each loop. This allows you to define multiple target values in Column A and output different corresponding results in Column B.
Will this macro work if I paste multiple values into Column A at once?
Yes, the provided code uses a 'For Each' loop to iterate through all cells within the target range. If you paste a block of data into Column A, it will evaluate each cell individually and update Column B accordingly.




