summing new value over the previous value based on more than item

Alaa mg

Active Member
Joined
May 29, 2021
Messages
375
Office Version
  1. 2019
hello
can I sum the new value in cell F6 over the prevouis value in cell F6 based on item in cell F2
F2 depends on column B , F6 depends on column C
when select A001 it will be
1.xlsx
ABCDEF
1ITEMCOSTITEM
2A0011000A001
3A0021500
4A0032900
5A004100TOTAL
61000
Sheet1

and when select different item should sum over previous value like this
1.xlsx
ABCDEF
1ITEMCOSTITEM
2A0011000A002
3A0021500
4A0032900
5A004100TOTAL
62500
Sheet1
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi Alaa,

Try this
VBA Code:
Sub GetTotal()
    Dim WledVal As Long
    WledVal = Application.VLookup(Range("F2"), Range("B2:C5"), 2, 0)
    Range("F6").Value = Range("F6").Value + WledVal
End Sub
 
Upvote 0
I might be misreading the requirement but if you just need a formula try this:

Book1
ABCDEF
1ITEMCOSTITEM
2A0011000A003
3A0021500
4A0032900
5A004100TOTAL
65400
Sheet1
Cell Formulas
RangeFormula
F6F6=SUM($C$2:INDEX($C$2:$C$5,MATCH($F$2,$B$2:$B$5,0)))
 
Upvote 0
@Sahak gives error mismatch in this line
VBA Code:
 WledVal = Application.VLookup(Range("F2"), Range("B2:C5"), 2, 0)
 
Upvote 0
@Alex Blakenburg based on your picture and what I want . the value should be 2900 and if I select another like A001 then the summing is 2900+1000=3900 and if I clear the cell F2 should start from the beginning when sum .
 
Upvote 0
The error you got with @Sahak's code indicates an invalid Item was entered into F2.

Here is a slightly more elaborate version to be copied into the Code Module of the sheet you are working on using a Change event.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    Dim inputCell As Range, totalCell As Range, dataRng As Range
    Dim addnewVal As Double
    Dim lastRow As Long

    Set inputCell = Range("F2")
    Set totalCell = Range("F6")
    lastRow = Range("B" & Rows.Count).End(xlUp).Row
    Set dataRng = Range("B2:C" & lastRow)

    If Not Intersect(Target, inputCell) Is Nothing Then
        If inputCell = "" Then
            totalCell.Value = ""
        Else
            With Application
                If IsError(.VLookup(inputCell, dataRng, 2, False)) Then
                    MsgBox "Invalid Item Entered"
                Else
                    addnewVal = .IfError(.VLookup(inputCell, dataRng, 2, False), 0)
                    totalCell.Value = totalCell.Value + addnewVal
                End If
            End With
        End If

    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Because you want to access the previous value in the cell that has the formula in it, I don't believe any formula will work.
I don't know enough about the new Lambda function to know whether it might be able to handle it but to even try that you would need to be on MS 365 and be quite current in terms of installed updates.
 
Upvote 0

Forum statistics

Threads
1,223,786
Messages
6,174,548
Members
452,572
Latest member
KP53

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