Subtraction Without Formula Independently for Each Row

excelnoobiez

New Member
Joined
Aug 8, 2022
Messages
3
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I currently have a range of cells I2:M2. There are no formulas in these cells, just values. I need to input a number in the corresponding cell in column H, hit ENTER, and have this number subtracted from the cells in column I2:M2, and display the result.

Example: I2 contains the number 15000. I type in the number 250 in cell H2 and hit ENTER. I2 should change to number 14750. J2 goes from 30000 to 29750. K2 goes from 45000 to 44750. L2 goes from 60000 to 59750. M2 goes from 150000 to 149750.

I need each row to have its own code so that the number I enter in column in H only affects that specific row.

Example: I3 contains the number 15000. I type in the number 250 in cell H3 and hit ENTER. I3 should change to number 14750. J3 goes from 30000 to 29750. K3 goes from 45000 to 44750. L3 goes from 60000 to 59750. M3 goes from 150000 to 149750.

Can someone help with this? I'm sure it has to be done in code.

Thank you in advance!
 

Attachments

  • Capture.PNG
    Capture.PNG
    47.2 KB · Views: 6

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You can probably use the Worksheet_Change event like so:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    Dim ce As Range
    Application.EnableEvents = False
        For Each ce In Range("I" & Target.Row).Resize(, 5)
            ce.Value = ce.Value - Target.Value
        Next ce
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Another option without the (albeit small) loop:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    Application.EnableEvents = False
    Target.Copy
    Range("I" & Target.Row).Resize(, 5).PasteSpecial Paste:=xlPasteAll, Operation:=xlSubtract
    Application.CutCopyMode = False
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
You can probably use the Worksheet_Change event like so:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    Dim ce As Range
    Application.EnableEvents = False
        For Each ce In Range("I" & Target.Row).Resize(, 5)
            ce.Value = ce.Value - Target.Value
        Next ce
    Application.EnableEvents = True
End If
End Sub
This works well, thank you. Is there a way to have the cell automatically delete its contents after the command is run?
 
Upvote 0
Yes, after the "Next ce" line, before re-enabling events, add:
VBA Code:
Target.Value = ""
If using the second code option I provided (without the For/Next loop), add that line between the 'CutCopyMode' and 'EnableEvents' lines.
 
Upvote 0
Solution
Yes, after the "Next ce" line, before re-enabling events, add:
VBA Code:
Target.Value = ""
If using the second code option I provided (without the For/Next loop), add that line between the 'CutCopyMode' and 'EnableEvents' lines.
Perfect thanks!
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,699
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top