VBA & Macro Problems

Use Excel VBA to Set A1 to Zero When B1 Changes to 1

Ayan MasoodAyan Masood Nov 12, 2026 870 views

Question details

Set cell A1 to 0 automatically when cell B1 is changed to 1, without changing A1 when B1 is 0.

Use Excel VBA to Set A1 to Zero When B1 Changes to 1
Product
Microsoft Excel VBA worksheet macro
Device & OS
not provided
Scenario
The workbook needs a cell-specific automation rule while retaining the ability to enter or calculate A1 normally when B1 is 0.
Observed behavior
A1 should be overwritten with 0 only after B1 is changed to the value 1.
Before you start

Use the desktop version of Excel and save the workbook as an Excel Macro-Enabled Workbook (*.xlsm). Confirm whether B1 is typed manually or is the result of a formula, because each case uses a different worksheet event.

Solution 1Recommended

Add a Worksheet_Change macro when B1 is entered manually

Use this method when a user types, pastes, or otherwise directly changes the value in B1.

The code belongs in the worksheet's code module, not in a standard VBA module. It temporarily disables events while writing 0 to A1, preventing another change event from being triggered by the macro itself.

1
Open the worksheet code module

Right-click the tab for the worksheet that contains A1 and B1, then select View Code.

2
Paste the worksheet event code

In the code window, paste this procedure: Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo CleanUp If Intersect(Target, Me.Range("B1")) Is Nothing Then Exit Sub If Me.Range("B1").Value = 1 Then Application.EnableEvents = False Me.Range("A1").Value = 0 End If CleanUp: Application.EnableEvents = True End Sub

3
Save and enable the macro

Select File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm), and enable macros when Excel prompts you to do so.

4
Test the rule

Return to the worksheet and enter 1 in B1. Excel sets A1 to 0. Entering 0 in B1 does not change the current value in A1.

Add a Worksheet_Change macro when B1 is entered manually
A1 remains unchanged when B1 is 0: The macro writes to A1 only when B1 equals 1. It does not restore a previous A1 value if B1 is later changed back to 0.
Automate spreadsheet actions with WPS Office

Create a Cell-Change Macro in WPS Spreadsheets

WPS Spreadsheets provides a familiar worksheet-based macro workflow for compatible XLSM files. Open the worksheet code area, add an event macro that checks B1, and save the workbook in a macro-enabled format to automate A1 when the condition is met.

  1. 1. Open the macro-enabled workbook: Launch WPS Spreadsheets and open the XLSM workbook containing the A1 and B1 cells.
  2. 2. Access the worksheet macro editor: Use the Developer or Macro tools to open the VBA-compatible editor, then select the code module for the relevant worksheet.
  3. 3. Add the cell-checking event procedure: Insert a worksheet event procedure that checks B1 and assigns 0 to A1 when B1 equals 1.
  4. 4. Save in a macro-enabled format: Choose Save As and retain the XLSM format so the workbook can preserve its macro content.
Automate worksheet actions when a specified cell changes value.Use a familiar spreadsheet interface for editing cells, formulas, and macro-enabled workbooks.Open and edit Microsoft Excel XLSM, XLSX, and XLS files with strong format compatibility.Continue working with Excel-style formulas and workbook layouts after migration.
microsoft office alternative - wps office

Frequently Asked Questions

Why does the code need to be placed in the worksheet module?

Worksheet_Change and Worksheet_Calculate are worksheet events. Right-click the specific sheet tab, select View Code, and place the procedure there so it monitors that sheet's B1 cell.

Will changing B1 back to 0 restore the old value in A1?

No. The supplied macro leaves A1 unchanged when B1 is 0. If you need to restore a prior value, the macro must store that value separately before setting A1 to 0.

Why does my Worksheet_Change macro not run when B1 has a formula?

Worksheet_Change runs only after direct edits, such as typing or pasting. When B1 changes because its formula recalculates, use the Worksheet_Calculate event instead.

Why is Application.EnableEvents set back to True in the code?

Writing to A1 can trigger additional worksheet events. The error-handling cleanup section restores events even if the macro encounters an error, so other Excel event macros continue working.

Do I need to save the file as XLSM?

Yes. Use File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). Saving as XLSX removes VBA code from the workbook.