EDIT: original post asked about adding values. Correct question regards multiplication.
Hello everyone
Here's a bit of VBA I found online to calculate a weighted median.
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
The code works nicely, except in cases where the cells in WeightRange are between 0 and 1. So it works fine if the cells in WeightRange are 1.0 and above, but the function returns a Value error if they WeightRange has anything like 0.6, 0.3, etc.
Is there any way of multiplying 100 for each cell in WeightRange while the function runs? I have tried dropping in
For WeightRangeCell In WeightRange
WeightRangeCell = WeightRangeCell*100
Next
but it doesn't seem to do the trick (then again, I don't know VBA). Seems like something that ought to be simple enough.
Unfortunately, adding a helper column is not an option, as I have hundreds of these weight ranges....
Thanks in advance!
Bookmarks