How to sum a row when the sum of a different row equals a certain value, and only between two dates

drmcbiz

New Member
Joined
Jul 20, 2023
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

Sorry for the confusing title. Having problems figuring this out elegantly. I've got a huge workbook so I was hoping to solve this in one cell (as I need to solve this thousands of times in the one workbook and having multiple rows of formula to figure out the answer keeps making my Excel crash!).

What I'm trying to do is this:

Between the dates of 2/1/23 and 9/1/23 I want to sum up the profit made (column D) on the first $500 of notes exercised (column C). The answer here should be $20 (highlighted in green).

Thanks!


Excel problem.png
 
Welcome to the MrExcel board!

Will this number always occur exactly by summing column B from the start date?
For example, with the sample data from post 31, could you be looking to sum the profits on, say, the first $350 of notes exercised?
Correct - both the amount of notes to be summed and the dates to sum between will change. I might just want to sum with first $200 of notes exercised between 2/1 and the 4/1 for example.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Correct - both the amount of notes to be summed and the dates to sum between will change. I might just want to sum with first $200 of notes exercised between 2/1 and the 4/1 for example.
But this example ($200) still is an exact amount of 2 rows at $100. My example was $350. If that is possible then by my understanding the suggestion in post #7 does not produce the correct result.
Here it is below. Shouldn't the result in that case be 14 not 9 since you would nee to add rows 109:112 to get up to (& past) $350 in column B?

drmcbiz.xlsm
ABCD
1081/01/202310010
1092/01/20231002100
1103/01/20231003200
1114/01/20231004300
1125/01/20231005400
1136/01/20231006500
1147/01/20231007600
1158/01/20231008700
1169/01/20231009800
117TOTAL9
118
119Limit350
120Start Date2/01/2023
121End Date9/01/2023
Sheet2
Cell Formulas
RangeFormula
D108D108=IF(AND(A108>=B$120,A108<=B$121),B$108,0)
D109:D116D109=IF(AND(A109>=B$120,A109<=B$121),SUM(D108,B109),0)
C117C117=SUMIFS(C108:C116,A108:A116,">="&B120,A108:A116,"<="&B121,D108:D116,"<="&B$119)
 
Upvote 0
Hi Peter,

Sorry, yes you're right there.

I'm trying to obtain that answer (14) using once cell (without the addition of column D)
 
Upvote 0
I don't think that you will like this any better than using helper cells. It is particularly long since you are using an old version of Excel.

drmcbiz.xlsm
ABC
1
2
31/01/20231001
42/01/20231002
53/01/20231003
64/01/20231004
75/01/20231005
86/01/20231006
97/01/20231007
108/01/20231008
119/01/20231009
12
13
14Start2/01/2023
15End9/01/2023
16First $500
17Sum20
Sheet1
Cell Formulas
RangeFormula
B17B17=SUM(INDEX(C3:C11,MATCH(B14,A3:A11,0)):INDEX(C3:C11,MATCH(B14,A3:A11,0)+MATCH(B16,SUMIF(INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),"<="&INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),INDEX($B$3:$B$11,MATCH(B14,A3:A11,0)):INDEX(B3:B11,MATCH(B15,A3:A11,0))))+ISNA(MATCH(B16,SUMIF(INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),"<="&INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),INDEX($B$3:$B$11,MATCH(B14,A3:A11,0)):INDEX(B3:B11,MATCH(B15,A3:A11,0))),0))-1))


drmcbiz.xlsm
ABC
1
2
31/01/20231001
42/01/20231002
53/01/20231003
64/01/20231004
75/01/20231005
86/01/20231006
97/01/20231007
108/01/20231008
119/01/20231009
12
13
14Start2/01/2023
15End9/01/2023
16First $350
17Sum14
Sheet1
Cell Formulas
RangeFormula
B17B17=SUM(INDEX(C3:C11,MATCH(B14,A3:A11,0)):INDEX(C3:C11,MATCH(B14,A3:A11,0)+MATCH(B16,SUMIF(INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),"<="&INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),INDEX($B$3:$B$11,MATCH(B14,A3:A11,0)):INDEX(B3:B11,MATCH(B15,A3:A11,0))))+ISNA(MATCH(B16,SUMIF(INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),"<="&INDEX($A$3:$A$11,MATCH(B14,A3:A11,0)):INDEX(A3:A11,MATCH(B15,A3:A11,0)),INDEX($B$3:$B$11,MATCH(B14,A3:A11,0)):INDEX(B3:B11,MATCH(B15,A3:A11,0))),0))-1))
 
Upvote 0
Solution
You could compare this user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the mini sheet.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

At this stage I have assumed that there will be sufficient rows with suitable dates to achieve the dollar amount of notes exercised.
If it is possible that there is not enough rows in the date range to achieve the dollar amount of notes exercised (eg with the same dates below and the value in row 16 was, say, 2000) what should happen?

Also, if it is possible that the start date is later than the latest date in column A, what should happen?

VBA Code:
Function ProfitSum(rng As Range, dStart As Date, NotesExercised As Double) As Double
  Dim a As Variant
  Dim i As Long, j As Long
  Dim NotesSum As Double
 
  a = rng.Value
  i = 1
  Do Until a(i, 1) >= dStart
    i = i + 1
  Loop
  Do Until NotesSum >= NotesExercised
    NotesSum = NotesSum + a(i + j, 2)
    ProfitSum = ProfitSum + a(i + j, 3)
    j = j + 1
  Loop
End Function

drmcbiz.xlsm
ABC
1
2
31/01/20231001
42/01/20231002
53/01/20231003
64/01/20231004
75/01/20231005
86/01/20231006
97/01/20231007
108/01/20231008
119/01/20231009
12
13
14Start2/01/2023
15End9/01/2023
16First $500350
17Sum2014
Sheet1
Cell Formulas
RangeFormula
B17:C17B17=ProfitSum($A$3:$C$11,$B$14,B16)
 
Upvote 0
Thanks for the answers so far everyone, much appreciated.

Post #7 from Bosquedeguate gets the answer I'm after by adding the extra column. However, if I add this column to my workbook it will increase its size many fold and the system crashes.

Is it possible to solve within 1 cell without adding the extra column?

I have only basic VBA knowledge, but the inputs (note amount and profit) are dynamic and hence I imagine VBA would not update these continuously / be appropriate?

Thanks
The VBA function is like any standard Excel function. … when you change a value the is a function argument it recalculates the cell(s) where you have it.

Also, if the $500 sum limit will not change that can be a constant in the VBA code, and eliminated as an argument. Or … if the Limit, Start Date, and End Date will always be in fixed cells - These arguments to the function can be removed. The code would always grab the 3 values from a fixed cells on the sheet containing the values.
Just need your clarification and direction on this.
 
Upvote 0
The VBA function is like any standard Excel function. … when you change a value the is a function argument it recalculates the cell(s) where you have it.

Also, if the $500 sum limit will not change that can be a constant in the VBA code, and eliminated as an argument. Or … if the Limit, Start Date, and End Date will always be in fixed cells - These arguments to the function can be removed. The code would always grab the 3 values from a fixed cells on the sheet containing the values.
Just need your clarification and direction on this.
Again a VBA function eliminates the need for an extra column in the Notes table. Adding VBA code to you Excel file is easy, but it does require saving the file as a Macro-enabled file.
 
Upvote 0
Amazing, thank-you all for your help. Peter_SSs formula has solved it in a way which works for my sheet.
 
Upvote 0
Peter_SSs formula has solved it in a way which works for my sheet.
Glad it worked for you. Thanks for letting us know. (y)

I'm a bit surprised though that given the comment below that huge formula evaluated so many times doesn't bog your sheet down. :unsure:
.. as I need to solve this thousands of times in the one workbook
 
Upvote 0
Hi Peter,

It certainly slows my sheet down (takes about 30 seconds to update each time I change a variable). However, I was able to get the answer i needed (where adding the extra column crashed the sheet entirely!)
 
Upvote 0

Forum statistics

Threads
1,215,297
Messages
6,124,113
Members
449,142
Latest member
championbowler

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