Hi folks,
I have the below vba code which is calculating the sum of quantity against the items on click of the button from the "data" sheet and populate the results onto the "output" sheet. I need to calculate the median price also but struggling to do so. Can someone help on this please? I am attaching the sample data with code for your easy reference. Thanks in advance...
Sub Test()
Dim vT, v
Dim i As Long, ndx As Long, d As Object
Dim t As Double, tp As Double
vT = Range("Data!A1").CurrentRegion.Value2
Set d = CreateObject("Scripting.Dictionary")
With Worksheets("Output")
.Cells(1, 1).CurrentRegion.Offset(1).Clear
'Item and Descriptions
For i = 2 To UBound(vT)
d.Item(vT(i, 1)) = vT(i, 2)
Next i
.Cells(2, 1).Resize(d.Count) = Application.Transpose(d.keys)
.Cells(2, 2).Resize(d.Count) = Application.Transpose(d.items)
ReDim v(1 To d.Count, 1 To 5)
tp = 0
For i = 2 To UBound(vT)
ndx = Application.Match(vT(i, 1), d.keys, 0)
v(ndx, 1) = v(ndx, 1) + vT(i, 3) 'Quantity
Next
d.RemoveAll
.Cells(2, 3).Resize(UBound(v, 1), UBound(v, 2)).Value = v
With .UsedRange
.Columns("A").NumberFormat = "@"
.Columns("B").NumberFormat = "#,##0"
.Columns("C").NumberFormat = "@"
.Columns("D").NumberFormat = "$* #,##0.00"
End With
End With
End Sub
Thanks,
Abhi
Bookmarks