add / subtract new price in cell to multiple cells in column based on two cells values

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
Hello,
I want macro to add new price to multiple cells in column E based on H2 after adding should clear H2 cell
example:
Microsoft Excel‫‬.xlsx
ABCDEFGHI
1ITEMCODEBRANDQTYUNIT PRICETOTALADDSUBTRACT
211221BS 205/70R15C R623 THI62.00405.0025,110.00
321227BS 215/70R15C R623 THI22.00425.009,350.00
431227BS 215/70R15C R623 THI4.00544.002,176.00
541241BS 1200R20 G580 JAP220.002,035.00447,700.00
651284GC 1200R20 AZ0026 CHI80.00895.0071,600.00
761284GC 1200R20 AZ0026 CHI50.001,125.0056,250.00
871285GC 1200R20 AZ0183 CHI40.00925.0037,000.00
981285GC 1200R20 AZ0183 CHI10.001,225.0012,250.00
1091285GC 1200R20 AZ0183 CHI60.001,205.0072,300.00
11101385GC 315/80R22.5 AT161 CHI20.00735.0014,700.00
12111294GC 315/80R22.5 AZ188 CHI20.00745.0014,900.00
13121287GC 315/80R22.5 AZ126 CHI60.00735.0044,100.00
14131305BS 700R16 R230 JAP2.00770.001,540.00
15141306BS 750R16 R230 JAP10.00775.007,750.00
16151306BS 750R16 R230 JAP4.00780.003,120.00
17TOTAL819,846.00
BRANDS
Cell Formulas
RangeFormula
F2:F16F2=D2*E2
F17F17=SUM(F2:F16)



fill H2
Microsoft Excel‫‬.xlsx
ABCDEFGHI
1ITEMCODEBRANDQTYUNIT PRICETOTALADDSUBTRACT
211221BS 205/70R15C R623 THI62.00405.0025,110.0010
321227BS 215/70R15C R623 THI22.00425.009,350.00
431227BS 215/70R15C R623 THI4.00544.002,176.00
541241BS 1200R20 G580 JAP220.002,035.00447,700.00
651284GC 1200R20 AZ0026 CHI80.00895.0071,600.00
761284GC 1200R20 AZ0026 CHI50.001,125.0056,250.00
871285GC 1200R20 AZ0183 CHI40.00925.0037,000.00
981285GC 1200R20 AZ0183 CHI10.001,225.0012,250.00
1091285GC 1200R20 AZ0183 CHI60.001,205.0072,300.00
11101385GC 315/80R22.5 AT161 CHI20.00735.0014,700.00
12111294GC 315/80R22.5 AZ188 CHI20.00745.0014,900.00
13121287GC 315/80R22.5 AZ126 CHI60.00735.0044,100.00
14131305BS 700R16 R230 JAP2.00770.001,540.00
15141306BS 750R16 R230 JAP10.00775.007,750.00
16151306BS 750R16 R230 JAP4.00780.003,120.00
17TOTAL819,846.00
BRANDS
Cell Formulas
RangeFormula
F2:F16F2=D2*E2
F17F17=SUM(F2:F16)


RESULT



Microsoft Excel‫‬.xlsx
ABCDEFGHI
1ITEMCODEBRANDQTYUNIT PRICETOTALADDSUBTRACT
211221BS 205/70R15C R623 THI72.00405.0029,160.00
321227BS 215/70R15C R623 THI32.00425.0013,600.00
431227BS 215/70R15C R623 THI14.00544.007,616.00
541241BS 1200R20 G580 JAP230.002,035.00468,050.00
651284GC 1200R20 AZ0026 CHI90.00895.0080,550.00
761284GC 1200R20 AZ0026 CHI60.001,125.0067,500.00
871285GC 1200R20 AZ0183 CHI50.00925.0046,250.00
981285GC 1200R20 AZ0183 CHI20.001,225.0024,500.00
1091285GC 1200R20 AZ0183 CHI70.001,205.0084,350.00
11101385GC 315/80R22.5 AT161 CHI30.00735.0022,050.00
12111294GC 315/80R22.5 AZ188 CHI30.00745.0022,350.00
13121287GC 315/80R22.5 AZ126 CHI70.00735.0051,450.00
14131305BS 700R16 R230 JAP12.00770.009,240.00
15141306BS 750R16 R230 JAP20.00775.0015,500.00
16151306BS 750R16 R230 JAP14.00780.0010,920.00
17TOTAL953,086.00
BRANDS
Cell Formulas
RangeFormula
F2:F16F2=D2*E2
F17F17=SUM(F2:F16)

and when fill I2 then should subtract from Cells in column E for each brand and clear I2
like this
Microsoft Excel‫‬.xlsx
ABCDEFGHI
1ITEMCODEBRANDQTYUNIT PRICETOTALADDSUBTRACT
211221BS 205/70R15C R623 THI72.00405.0029,160.0010
321227BS 215/70R15C R623 THI32.00425.0013,600.00
431227BS 215/70R15C R623 THI14.00544.007,616.00
541241BS 1200R20 G580 JAP230.002,035.00468,050.00
651284GC 1200R20 AZ0026 CHI90.00895.0080,550.00
761284GC 1200R20 AZ0026 CHI60.001,125.0067,500.00
871285GC 1200R20 AZ0183 CHI50.00925.0046,250.00
981285GC 1200R20 AZ0183 CHI20.001,225.0024,500.00
1091285GC 1200R20 AZ0183 CHI70.001,205.0084,350.00
11101385GC 315/80R22.5 AT161 CHI30.00735.0022,050.00
12111294GC 315/80R22.5 AZ188 CHI30.00745.0022,350.00
13121287GC 315/80R22.5 AZ126 CHI70.00735.0051,450.00
14131305BS 700R16 R230 JAP12.00770.009,240.00
15141306BS 750R16 R230 JAP20.00775.0015,500.00
16151306BS 750R16 R230 JAP14.00780.0010,920.00
17TOTAL953,086.00
BRANDS
Cell Formulas
RangeFormula
F2:F16F2=D2*E2
F17F17=SUM(F2:F16)


result
Microsoft Excel‫‬.xlsx
ABCDEFGHI
1ITEMCODEBRANDQTYUNIT PRICETOTALADDSUBTRACT
211221BS 205/70R15C R623 THI62.00405.0025,110.00
321227BS 215/70R15C R623 THI22.00425.009,350.00
431227BS 215/70R15C R623 THI4.00544.002,176.00
541241BS 1200R20 G580 JAP220.002,035.00447,700.00
651284GC 1200R20 AZ0026 CHI80.00895.0071,600.00
761284GC 1200R20 AZ0026 CHI50.001,125.0056,250.00
871285GC 1200R20 AZ0183 CHI40.00925.0037,000.00
981285GC 1200R20 AZ0183 CHI10.001,225.0012,250.00
1091285GC 1200R20 AZ0183 CHI60.001,205.0072,300.00
11101385GC 315/80R22.5 AT161 CHI20.00735.0014,700.00
12111294GC 315/80R22.5 AZ188 CHI20.00745.0014,900.00
13121287GC 315/80R22.5 AZ126 CHI60.00735.0044,100.00
14131305BS 700R16 R230 JAP2.00770.001,540.00
15141306BS 750R16 R230 JAP10.00775.007,750.00
16151306BS 750R16 R230 JAP4.00780.003,120.00
17TOTAL819,846.00
BRANDS
Cell Formulas
RangeFormula
F2:F16F2=D2*E2
F17F17=SUM(F2:F16)

when the cells H2,I2 are empty, then nothing happens.
one of cell will be filled(not together filled)
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If you have to use a macro here's one way (needs to be run while on the sheet with the data):

VBA Code:
Option Explicit
Sub Macro1()

    Dim i As Long, j As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        On Error Resume Next
            .ShowAllData
        On Error GoTo 0
        .Columns.EntireColumn.Hidden = False
        .Rows.EntireRow.Hidden = False
        j = .Cells(Rows.Count, "D").End(xlUp).Row
        For i = 2 To j
            .Range("D" & i).Value = Range("D" & i).Value + Range("H2").Value - Range("I2").Value
        Next i
    End With
    
    Range("H2:I2").ClearContents
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Use this in Worksheet module (not in general module (sub))
Triggered once typing value into those 2 cells

PHP:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If Intersect(Target, Range("H2:I2")) Is Nothing Or Target.Count > 1 Then Exit Sub
If Not IsNumeric(Target) Or (Range("H2") <> "" And Range("I2") <> "") Then Exit Sub
If Range("H2") = "" And Range("I2") = "" Then Exit Sub
If MsgBox("Qty will be added/subtracted by " & (Range("H2") + Range("I2")) & ". Do you want to continue?", vbYesNo) = vbNo Then Exit Sub
For Each cell In Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
    cell = cell + Range("H2") - Range("I2")
Next
End Sub
 
Upvote 0
Hi bebo021999,

Nice code 👍

I would also use EnableEvents to stop the macro firing off again when the cell values in Col. D are changed:

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    If Intersect(Target, Range("H2:I2")) Is Nothing Or Target.Count > 1 Then Exit Sub
    If Not IsNumeric(Target) Or (Range("H2") <> "" And Range("I2") <> "") Then Exit Sub
    If Range("H2") = "" And Range("I2") = "" Then Exit Sub
    If MsgBox("Qty will be added/subtracted by " & (Range("H2") + Range("I2")) & ". Do you want to continue?", vbYesNo) = vbNo Then Exit Sub
    Application.EnableEvents = False
    For Each cell In Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
        cell = cell + Range("H2") - Range("I2")
    Next cell
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

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