Dynamic Range

HMN93

New Member
Joined
May 31, 2014
Messages
20
Hello,
I would like to ask for help on this issue.
I have a list of items and cost and I need to find the median for every set of item
example:

itemCostMedian
apple3apple3.75
apple2.8orange4.3
apple5.3
apple4.5
orange2.3
orange4.3
orange5.7
orange4.3
orange3.5
orange6.1
orange3.9

<tbody>
</tbody>

I am trying to do this in excel vba
how can I write the macro to find the median for each item?
I thought of a for loop but I am stuck when it comes to how to write the range so I will be dynamic

Any help is greatly appreciated!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Do you require VBA? There is a simple formula solution which I'll provide in case it suffices.

Does this meet your needs?

<
Excel 2012
ABCDEF
1itemCostMedianMedian
2apple3apple3.93.9
3apple2.8orange4.34.3
4apple5.3pear5151
5apple4.5mango110110
6orange2.3
7orange4.3
8orange5.7
9orange4.3
10orange3.5
11orange6.1
12orange3.9
13pear51
14mango110
Sheet49
Cell Formulas
RangeFormula
E2=SUMIFS($B$2:$B$14,$A$2:$A$14,D2)/COUNTIFS($A$2:$A$14,D2)
E3=SUMIFS($B$2:$B$14,$A$2:$A$14,D3)/COUNTIFS($A$2:$A$14,D3)
E4=SUMIFS($B$2:$B$14,$A$2:$A$14,D4)/COUNTIFS($A$2:$A$14,D4)
E5=SUMIFS($B$2:$B$14,$A$2:$A$14,D5)/COUNTIFS($A$2:$A$14,D5)
F2=SUMIFS($B:$B,$A:$A,D2)/COUNTIFS($A:$A,D2)
F3=SUMIFS($B:$B,$A:$A,D3)/COUNTIFS($A:$A,D3)
F4=SUMIFS($B:$B,$A:$A,D4)/COUNTIFS($A:$A,D4)
F5=SUMIFS($B:$B,$A:$A,D5)/COUNTIFS($A:$A,D5)
 
Upvote 0
DRSteele, thank you for your reply.
doesn't the sum/count return the mean? As for using vba, the next step is to calculate MAD (median absolute deviation) and this computation is part of a long excel sheet and I need to use vba to compute everything.

 
Last edited:
Upvote 0
Yes, it counts mean. I misread - my apologies. I cannot help with VBA...sorry and good luck.
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,903
Members
449,477
Latest member
panjongshing

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