PC Review


Reply
Thread Tools Rate Thread

Calculating the Median Price Sold

 
 
AMB
Guest
Posts: n/a
 
      22nd Aug 2008
Hello,

I am trying to find a reasonable way of calculating a median. I have a data
set that is about 20,000 lines long and it contains Quantity Sold and Unit
Price. I need to calculate out the Median Price Sold. Is there a way to
count the Unit Price mulitple times based on the Quantity Sold so that I end
up with an accurate Median?

The dataset appears as follows:

Quantity Sold Unit Price
1 330.00
5 300.00
3 330.00
5 360.00
4 360.00
4 330.00
3 369.00
1 374.00

Regards,

Adam


--
Adam Brody
National Business Analyst
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      22nd Aug 2008
You need a UDF. I copied this a long time ago and can't remember the name of
the original author so apologies to whoever it was.

Alt +F11 to open VB editor, Double click 'This Workbook' and paste this in
on the right.

Function WeightedMedian(ValueRange As Range, WeightRange As Range)
Dim MedianArray()
On Error GoTo WrongRanges
ArrayLength = Application.Sum(WeightRange)
ReDim MedianArray(1 To ArrayLength)
Counter = 0
ArrayCounter = 0
For Each ValueRangeCell In ValueRange
LoopCounter = LoopCounter + 1
FirstArrayPos = ArrayCounter + 1
ArrayCounter = ArrayCounter + Application.Index(WeightRange,
LoopCounter)
For n = FirstArrayPos To ArrayCounter
MedianArray(n) = ValueRangeCell.Value
Next
Next
WeightedMedian = Application.Median(MedianArray)
Exit Function
WrongRanges:
WeightedMedian = CVErr(2016)
End Function

call with
=WeightedMedian(B1:B8,A1:A8)
It returns 345

Mike


WrongRanges:
WeightedMedian = CVErr(2016)
End Function


"AMB" wrote:

> Hello,
>
> I am trying to find a reasonable way of calculating a median. I have a data
> set that is about 20,000 lines long and it contains Quantity Sold and Unit
> Price. I need to calculate out the Median Price Sold. Is there a way to
> count the Unit Price mulitple times based on the Quantity Sold so that I end
> up with an accurate Median?
>
> The dataset appears as follows:
>
> Quantity Sold Unit Price
> 1 330.00
> 5 300.00
> 3 330.00
> 5 360.00
> 4 360.00
> 4 330.00
> 3 369.00
> 1 374.00
>
> Regards,
>
> Adam
>
>
> --
> Adam Brody
> National Business Analyst

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      22nd Aug 2008
Found it

With respect to the original author
David Hager

Mike

"Mike H" wrote:

> You need a UDF. I copied this a long time ago and can't remember the name of
> the original author so apologies to whoever it was.
>
> Alt +F11 to open VB editor, Double click 'This Workbook' and paste this in
> on the right.
>
> Function WeightedMedian(ValueRange As Range, WeightRange As Range)
> Dim MedianArray()
> On Error GoTo WrongRanges
> ArrayLength = Application.Sum(WeightRange)
> ReDim MedianArray(1 To ArrayLength)
> Counter = 0
> ArrayCounter = 0
> For Each ValueRangeCell In ValueRange
> LoopCounter = LoopCounter + 1
> FirstArrayPos = ArrayCounter + 1
> ArrayCounter = ArrayCounter + Application.Index(WeightRange,
> LoopCounter)
> For n = FirstArrayPos To ArrayCounter
> MedianArray(n) = ValueRangeCell.Value
> Next
> Next
> WeightedMedian = Application.Median(MedianArray)
> Exit Function
> WrongRanges:
> WeightedMedian = CVErr(2016)
> End Function
>
> call with
> =WeightedMedian(B1:B8,A1:A8)
> It returns 345
>
> Mike
>
>
> WrongRanges:
> WeightedMedian = CVErr(2016)
> End Function
>
>
> "AMB" wrote:
>
> > Hello,
> >
> > I am trying to find a reasonable way of calculating a median. I have a data
> > set that is about 20,000 lines long and it contains Quantity Sold and Unit
> > Price. I need to calculate out the Median Price Sold. Is there a way to
> > count the Unit Price mulitple times based on the Quantity Sold so that I end
> > up with an accurate Median?
> >
> > The dataset appears as follows:
> >
> > Quantity Sold Unit Price
> > 1 330.00
> > 5 300.00
> > 3 330.00
> > 5 360.00
> > 4 360.00
> > 4 330.00
> > 3 369.00
> > 1 374.00
> >
> > Regards,
> >
> > Adam
> >
> >
> > --
> > Adam Brody
> > National Business Analyst

 
Reply With Quote
 
AMB
Guest
Posts: n/a
 
      22nd Aug 2008
Mike,

Thanks for your assistance. It worked perfectly.

Adam



"Mike H" wrote:

> You need a UDF. I copied this a long time ago and can't remember the name of
> the original author so apologies to whoever it was.
>
> Alt +F11 to open VB editor, Double click 'This Workbook' and paste this in
> on the right.
>
> Function WeightedMedian(ValueRange As Range, WeightRange As Range)
> Dim MedianArray()
> On Error GoTo WrongRanges
> ArrayLength = Application.Sum(WeightRange)
> ReDim MedianArray(1 To ArrayLength)
> Counter = 0
> ArrayCounter = 0
> For Each ValueRangeCell In ValueRange
> LoopCounter = LoopCounter + 1
> FirstArrayPos = ArrayCounter + 1
> ArrayCounter = ArrayCounter + Application.Index(WeightRange,
> LoopCounter)
> For n = FirstArrayPos To ArrayCounter
> MedianArray(n) = ValueRangeCell.Value
> Next
> Next
> WeightedMedian = Application.Median(MedianArray)
> Exit Function
> WrongRanges:
> WeightedMedian = CVErr(2016)
> End Function
>
> call with
> =WeightedMedian(B1:B8,A1:A8)
> It returns 345
>
> Mike
>
>
> WrongRanges:
> WeightedMedian = CVErr(2016)
> End Function
>
>
> "AMB" wrote:
>
> > Hello,
> >
> > I am trying to find a reasonable way of calculating a median. I have a data
> > set that is about 20,000 lines long and it contains Quantity Sold and Unit
> > Price. I need to calculate out the Median Price Sold. Is there a way to
> > count the Unit Price mulitple times based on the Quantity Sold so that I end
> > up with an accurate Median?
> >
> > The dataset appears as follows:
> >
> > Quantity Sold Unit Price
> > 1 330.00
> > 5 300.00
> > 3 330.00
> > 5 360.00
> > 4 360.00
> > 4 330.00
> > 3 369.00
> > 1 374.00
> >
> > Regards,
> >
> > Adam
> >
> >
> > --
> > Adam Brody
> > National Business Analyst

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inventory Add, Cost, Sold, Sold price - formula Summer Microsoft Excel Misc 3 20th Jul 2008 06:26 PM
Calculating Median JOHNNY OUTING Microsoft Access Forms 1 29th Oct 2007 07:58 PM
I made a price list and now I want to type the word sold in red? =?Utf-8?B?S2F1YWkgQXJ0aXN0?= Microsoft Word New Users 2 28th Nov 2006 11:31 PM
Calculating a median Marco Shaw Microsoft Access Queries 3 10th Nov 2006 03:13 PM
Calculating a Median Mike Lempel Microsoft Access VBA Modules 2 3rd Mar 2004 07:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:51 AM.