VBA For Subtracting

ElPerson

New Member
Joined
Mar 16, 2017
Messages
24
Hi
I need a VBA code for Subtracting every cell in a column from the first cell in the same column

Thank you
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try this

Code:
Sub Subtracting()
    res = Range("B1")
    For i = 2 To Range("B" & Rows.Count).End(xlUp).Row
        res = res - Cells(i, "B")
    Next
    MsgBox "Result = " & res
End Sub
 
Upvote 0
you didn't specify column, but this is set for B
 
Upvote 0
Which column should this work??
Thank you

In cell B1 your first value, from B2 down your next values

e8e005210343519b476241b8594ffc62.jpg
 
Last edited:
Upvote 0
Try this

Code:
Sub Subtracting_2()
    MsgBox "Result = " & Range("B1") - WorksheetFunction.Sum(Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row))
End Sub
 
Upvote 0
Dear Sir
see the attached screenshot
i want every time i enter a value in the cells of the column B,D,F it substracted from a specific number say 1633 and the result updated in the A1 cell.
thank you
p0gqwHT
https://ibb.co/p0gqwHT
Try this

Code:
Sub Subtracting_2()
    MsgBox "Result = " & Range("B1") - WorksheetFunction.Sum(Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row))
End Sub
 
Upvote 0
Dear Sir
see the attached screenshot
i want every time i enter a value in the cells of the column B,D,F it substracted from a specific number say 1633 and the result updated in the A1 cell.
thank you
p0gqwHT
https://ibb.co/p0gqwHT


I do not understand. In cell A1 you have the initial value and in that same cell you want to write the result?

Your original request does not say much about what you need now.


I need a VBA code for Subtracting every cell in a column from the first cell in the same column


You could explain with your words what you have in A1 and how it will change each time you capture a data, explain with several examples.
 
Upvote 0
Excuse me , you are right, i changed my mind regarding what i want
NOW
i want the A1 cell to be updated every time a value in the cells of the column B,D,F is updated,
first i assign the starting value that we will subtract from it and in this case will be 1500,
then we start with b2 have 10 and d2 have 20 and f2 have 5
i want excel when the value in b2 inserted it subtracted from the 1500 and enter the result to A1 "1490" then when d2 value inserted 20 it subtract it from the A1 value "1490-20" > "1470 in A1"
so A1 now have 1470, next once the f2 value updated 5 the excel subtract that from the new A1 value "1470-5" and update the A1 with the result 1465.

I hope the idea is clear now
 
Upvote 0
Put the following code in the events of your sheet

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B2:F2")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        If Target.Value = "" Then Exit Sub
        Range("A1").Value = Range("A1").Value - Target.Value
    End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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