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
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
=SUMIFS(C2:C10,B2:B10,">="&B13,B2:B10,”>="&B14)
That doesn't appear to work at all for me.
What does it return for you?

This part:
”>="&B14
probably need to change to this:
VBA Code:
"<="&B14

There are two criteria that need to be applied - date criteria on the first column, but also dollar amount on the second column:
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).
 
Upvote 0
The criteria range should be the column for your dates. From your screen shot maybe that is column A.

Formula would be
=SUMIFS(C2:C10,A2:A10,">="&A13,A2:A10,”>="&A14)

I assuming your data is in Rows 2 to 10, and Columns A to C
 
Upvote 0
OK ... I am thinking there are two ways to solve this problem ...
  1. Add a 4th column to your data that flags it as 0 or a Running total if the date is within range
    Using 1 and 0 as your flag
    - Added Column D
    - Added a condition in the SUMIFS() parameters (see sample sheet below)
  2. Or, create a VBA function to do the work. I would the following arguments: Range of the Data (3 columns); Start Date, End Date, and Running Sum Limit
    - Assumes that the format and values in columns 1,2, & 3 will not change.
    - Would not require the addition of Column D calculation (in 1 above)
    Let us know if this is viable solution for you.
Sample:
GetLastRow.xlsm
ABCD
1081/1/202310010
1092/1/20231002100
1103/1/20231003200
1114/1/20231004300
1125/1/20231005400
1136/1/20231006500
1147/1/20231007600
1158/1/20231008700
1169/1/20231009800
117TOTAL20
118
119Limit500
120Start Date2/1/2023
121End Date9/1/2023
Sheet1
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
Here is a VBA Function that does the conditional Summing of Profit as needed.
Note this version of the code does not do any validation of the arguments it receives (to be added).

Please do some additional testing if you like this approach.

Were you expecting the function to also highlight the profit values that were used in the sum?

Using the same mini-sheet as above I used this function with the arguments below:
=SumConditional(A108:C116,B120,B121,B119)

GetLastRow.xlsm
ABC
1DateNote AmountProfit
2
31/1/20231001
42/1/20231002
53/1/20231003
64/1/20231004
75/1/20231005
86/1/20231006
97/1/20231007
108/1/20231008
119/1/20231009
12TOTAL20
13
14Limit500
15Start Date2/1/2023
16End Date9/1/2023
Sheet6
Cell Formulas
RangeFormula
C12C12=sumconditional(A3:C11,B15,B16,B14)


VBA Code:
Function SumConditional(rng As Range, Optional dtBeg As Date, Optional dtEnd As Date, Optional Limit)
  'Do some testing to validate argugments
  'rng 3 Columns wide
  'Column 1 of Range contain only date values
  'Column 2 and 3 contain only numeric values
  'Argument 2 (dtBeg) is a Date value - default value 1/1 of current year
  'Argument 3 (dtEnd) is a Date value - default value today's date
  'Limit is numeric - 99999999999
  
  
  Dim colDt, colNote, colProfit
  Dim rowStart, rowEnd
  Dim rngtmp As Range
  Dim sheet As Worksheet
  Dim nrows As Integer
  Dim noteSum
  Dim profiSum
  
  
  Set sheet = rng.Parent
  tmp = Split(rng.Address, ":")
  Set rngtmp = sheet.Range(tmp(0))
  colDt = rngtmp.Column
  rowStart = rngtmp.Row
  
  Set rngtmp = sheet.Range(tmp(1))
  colProfit = rngtmp.Column
  rowEnd = rngtmp.Row
  
  colNote = colProfit - colDt
  nrows = rng.Rows.Count
  
  
  
  For r = rowStart To rowEnd
    Set rngtmp = sheet.Cells(r, colDt)
    dttmp = rngtmp
    If dttmp >= dtBeg And dttmp <= dtEnd Then
      If noteSum < Limit Then
        noteSum = noteSum + sheet.Cells(r, colNote).Value
        profitSum = profitSum + sheet.Cells(r, colProfit).Value
      End If
    End If
  Next r
  
  SumConditional = profitSum
End Function
 
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
 
Upvote 0
Welcome to the MrExcel board!
I want to sum up the profit made (column D) on the first $500 of notes exercised
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?
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,256
Members
449,149
Latest member
mwdbActuary

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