VBA & Macro Problems

How to Decrease Column Values Based on Another Column using Excel VBA

Anum MushtaqAnum Mushtaq Oct 15, 2026 870 views

Question details

The user needs an Excel macro that automatically subtracts 10 from numerical values in Column D if the corresponding row in Column O contains the letter 'X'.

How to Decrease Column Values Based on a Condition Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Automating bulk numerical adjustments in a dataset where values in one column must change based on specific text criteria located in another column.
Observed behavior
Values in Column D are successfully decreased by 10 exclusively in the rows where Column O equals 'x'.
Before you start

Ensure that Column D contains only numerical values to avoid 'Type Mismatch' errors, and temporarily save a backup of your workbook since VBA macro changes cannot be easily undone.

Solution 1Recommended

Use a VBA For Loop Macro to Check Conditions and Decrease Values

Execute a VBA script that loops through your dataset from bottom to top, evaluating Column O and modifying Column D accordingly.

This solution utilizes a standard 'For' loop in VBA. It calculates the last used row in Column D and loops backward to row 2. Looping backward is generally a best practice when modifying cell structures or evaluating conditions in dynamic ranges.

1
Open the VBA Editor

Press the ALT + F11 shortcut keys simultaneously on your keyboard to open the Microsoft Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the top menu, click on 'Insert' and select 'Module' from the dropdown list. A new blank code window will appear.

3
Paste the VBA Code

Copy and paste the following code into the Module window: Sub Decrease() Dim Ttt As Long Dim Rrr As Long Ttt = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row For Rrr = Ttt To 2 Step -1 If Cells(Rrr, "O") = "x" Then Cells(Rrr, "D").Value = Cells(Rrr, "D").Value - 10 End If Next Rrr End Sub

4
Run the Macro

Close the VBA editor. In your worksheet, press ALT + F8 to open the Macro dialog box, select the 'Decrease' macro from the list, and click 'Run'.

Use a VBA For Loop Macro to Check Conditions and Decrease Values
Case Sensitivity: The provided code strictly checks for a lowercase 'x'. To make the macro case-insensitive so it detects both 'x' and 'X', change the condition to: If LCase(Cells(Rrr, "O")) = "x" Then
Powerful Macro Support

Run VBA Macros Smoothly with WPS Spreadsheet

WPS Office Spreadsheet provides robust support for VBA macros, allowing you to execute complex data manipulation scripts just like in Microsoft Excel, making your workflow incredibly efficient.

  1. 1. Install WPS Office: Download and install the free version of WPS Office from the official website.
  2. 2. Open Your Workbook: Launch WPS Spreadsheet and open the Excel file containing your dataset.
  3. 3. Access Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'Macros'.
  4. 4. Run the Code: Paste the Decrease macro script and click 'Run' to seamlessly process your data.
Fully compatible with Microsoft Excel macro formats (.xlsm)Free and lightweight alternative for daily office tasksBuilt-in Developer tools for editing and running VBA codesFamiliar user interface with zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why am I getting a 'Type Mismatch' error when running this macro?

This error occurs if there is non-numerical text, a space, or an error value in Column D where Column O contains 'x'. Ensure that all target cells in Column D contain only numbers before running the script.

How can I apply this macro to a different column?

You can easily modify the column letters in the VBA script. Simply change the 'D' and 'O' within the Cells(Rrr, "D") and Cells(Rrr, "O") statements in the code to your desired column letters.

How do I save an Excel file that contains a VBA macro?

You must save the file as an Excel Macro-Enabled Workbook (.xlsm). If you attempt to save it as a standard .xlsx file, Excel will prompt a warning, and the macro code will be permanently removed upon saving.

Can I make the macro check for both 'X' and 'Y'?

Yes, you can modify the IF statement to include an OR condition. Update the line to read: If Cells(Rrr, "O") = "x" Or Cells(Rrr, "O") = "y" Then.