Aggregate function with multiple conditions gives runtime error

Falck99

New Member
Joined
Feb 23, 2023
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have the 2016 Excel version where Minifs is not available. Instead, I'm trying to use the aggregate function to identify the minimum value of range C1:C4 in Sheet1 if two criteria are met (Range B1:B4 in Sheet1 equals value in cell B1 and Range A1:A4 equals value in cell A1). I am able to get the correct result if I "print" the formula in a cell in Sheet1, but get a runtime error 13 (type mismatch) when I specify the ranges in vba. I've written the following code:

Sub test()

Sheets("Sheet2").Range("a1") = Application.Aggregate(15, 6, Sheets("Sheet1").Range("c1:c4") / ((Sheets("sheet1").Range("b1:b4") = Sheets("sheet1").Range("b1")) * (Sheets("sheet1").Range("a1:a4") = Sheets("sheet1").Range("a1"))), 1)


End Sub


Would greatly appreciate if someone could point out the error, as I've now completely exhausted Google searching for an answer.

Thanks!
 

Attachments

  • Capture.PNG
    Capture.PNG
    4.1 KB · Views: 9

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Any of your source formatted as text?
Can you successfully write the formula in cell for just 1 cell?
and also,
Application.Aggregate(
Application.Worksheetfunction.Aggregate(

I will stop pretending after this. (that I know much of anything on Aggregate)
 
Upvote 0
Any of your source formatted as text?
Can you successfully write the formula in cell for just 1 cell?
and also,
Application.Aggregate(
Application.Worksheetfunction.Aggregate(

I will stop pretending after this. (that I know much of anything on Aggregate)
Everything with "General" format in Sheet1. Not entirely sure that I understand what you mean about writing the formula in cell. If I write the following in formula in excel (not using vba), it works as it should:

=AGGREGATE(15;6;C1:C4/((B1:B4=B1)*(A1:A4=A1));1)

I suspect that the error is related to the ranges in the vba code..

Also tried both application and application.worksheetfunction. Makes no difference unfortunately.

Appreciate you for trying to help
 
Upvote 0
Try this code
VBA Code:
Sub test()
Sheets("Sheet2").Range("a1") = Evaluate("aggregate(15,6,'Sheet1'!C1:c4/(('Sheet1'!b1:b4='Sheet1'!B1)*('Sheet1'!a1:a4='Sheet1'!a1)),1)")

End Sub
 
Upvote 0
Solution
Try this code
VBA Code:
Sub test()
Sheets("Sheet2").Range("a1") = Evaluate("aggregate(15,6,'Sheet1'!C1:c4/(('Sheet1'!b1:b4='Sheet1'!B1)*('Sheet1'!a1:a4='Sheet1'!a1)),1)")

End Sub
Thank you so much kvsrinivasamurthy! Works like a charm :)
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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