celicantes

New Member
Joined
Dec 4, 2018
Messages
3
Hello folks,

I am stuck with a VBA sumifs code in excel.

Basically, I am trying to get a SUMIFS that should retrieve for each date the value in the sum_range greater than the dates range previously input in a third cell.

In a normal formula would be:
=SUMIFS(SUM_RANGE, CRITERIA_RANGE, CRITERIA1, CRITERIA_RANGE, ">=" & STARTDATE, CRITERIA_RANGE, "<" & ENDDATE)

That's my code in VBA:

Sub test ()
For i = 4 To 368
StartDate = Sheets("Sheet1").Range("C5")
EndDate = Sheets("Sheet1").Range("C6")

bridge_t = Application.WorksheetFunction.SumIfs([iss_ceduto], [iss_date], Cells(i, 15), [iss_date], ">=" & StartDate, [iss_date], "<" & EndDate)

Cells(i, 17) = bridge_t

Next i
End sub

The code in VBA returns 0, while it works perfectly as a Formula.

Any idea how to help me?

Thanks in advance for any feedback.

Elio
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi Elio
Welcome to the board

You are using [iss_date] in the 3 criteria. Maybe it's a typo?

Code:
bridge_t = Application.WorksheetFunction.SumIfs([iss_ceduto],  [FONT=arial black][COLOR=#800000][iss_date][/COLOR][/FONT], Cells(i, 15), [FONT=arial black][COLOR=#800000][iss_date][/COLOR][/FONT], ">=" & StartDate,  [FONT=arial black][COLOR=#800000][iss_date][/COLOR][/FONT], "<" & EndDate)
 
Upvote 0
Hi Elio
Welcome to the board

You are using [iss_date] in the 3 criteria. Maybe it's a typo?

Code:
bridge_t = Application.WorksheetFunction.SumIfs([iss_ceduto],  [FONT=arial black][COLOR=#800000][iss_date][/COLOR][/FONT], Cells(i, 15), [FONT=arial black][COLOR=#800000][iss_date][/COLOR][/FONT], ">=" & StartDate,  [FONT=arial black][COLOR=#800000][iss_date][/COLOR][/FONT], "<" & EndDate)

Hi,

thank you for your reply.
Anyway, [iss_date] represents the "named" range with dates and it should theoretically work.
At least, in the excel formula it does.
 
Upvote 0
Hi

You have 3 conditions:

[iss_date] = Cells(i, 15)
[iss_date] >= StartDate
[iss_date] < EndDate

If [iss_date] must be equal to Cells(i, 15), this means that the formula will only sum if StartDate <= Cells(i, 15) < EndDate

Ex. Cells(i,15) = 1 Dec 2018

if StartDate is 2 Dec 2018 you don't have to use the SumIfs(), you already know that no record will match. Only records with date equal to 1 Dec 2018 satisfy the first criterion, but then they will not satisfy the second criterion.

Similar if EndDate is 30 Nov 2018. You know already that no record will match, because only dates equal to 1 Dec 2018 satisfy the first criterion and that's after the EndDate.

If this is really what you want, then your formula should be:

=IF(AND(CRITERIA1>=STARTDATE,CRITERIA1&LT;ENDDATE),SUMIFS(SUM_RANGE, CRITERIA_RANGE, CRITERIA1),0)

Does this make sense?
 
Upvote 0
Hi

You have 3 conditions:

[iss_date] = Cells(i, 15)
[iss_date] >= StartDate
[iss_date] < EndDate

If [iss_date] must be equal to Cells(i, 15), this means that the formula will only sum if StartDate <= Cells(i, 15) < EndDate

Ex. Cells(i,15) = 1 Dec 2018

if StartDate is 2 Dec 2018 you don't have to use the SumIfs(), you already know that no record will match. Only records with date equal to 1 Dec 2018 satisfy the first criterion, but then they will not satisfy the second criterion.

Similar if EndDate is 30 Nov 2018. You know already that no record will match, because only dates equal to 1 Dec 2018 satisfy the first criterion and that's after the EndDate.

If this is really what you want, then your formula should be:

=IF(AND(CRITERIA1>=STARTDATE,CRITERIA1<ENDDATE),SUMIFS(SUM_RANGE, CRITERIA_RANGE, CRITERIA1),0)

Does this make sense?

Yes, it does.
I added an if clause in the code and now it works fine.
Thank you a lot.

Elio
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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