ESto funciona.
FIN DE LA AYUDA.
Option Explicit
Sub P()
Dim t1 As Single
t1 = Timer()
Percentiles 0.5 ' Cambiar al percentil que queremos (0.5)
MsgBox "he finalizado el trabajo" & vbCrLf & "tiempo empleado: " & Timer() - t1
End Sub
Sub Percentiles(P As Double)
Dim H1 As Worksheet
Dim H2 As Worksheet
Dim UF As Long
Dim FilaPrimerDato As Long
Dim FilaUltimoDato As Long
Dim ADatos(10000) As Variant ' supongo que con 10001 será suficiente
Dim i As Long
Dim j As Long
Dim k As Long
Set H1 = Worksheets("Hoja1") ' hoja con los datos de entrada
Set H2 = Worksheets("Hoja2") ' hoja con los datos de salida
H2.Rows("2:10000").Delete ' borro los datos anteriores
With H1
UF = .Cells(1000000, 1).End(xlUp).Row
'ordenar por categoria y dato
' SUPONGO QUE LA CATEGORIA ESTÁ EN LA COLUMNA E
' Y EL DATO PARA EL PERCENTIL EN LA COLUMNA N
' el rango desde A:O
With .Sort.SortFields
.Clear
.Add2 Key:=Range("E2:E" & UF), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add2 Key:=Range("N2:N" & UF), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
With .Sort
.SetRange Range("A1:O" & UF)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
FilaPrimerDato = 2
FilaUltimoDato = UF
k = 0
j = 2
For i = 2 To UF
If .Cells(i + 1, 1) <> .Cells(i, 1) Then
FilaUltimoDato = i + 1
ADatos(k) = .Cells(i, 2)
k = k + 1
H2.Cells(j, 1) = .Cells(FilaPrimerDato, 1)
H2.Cells(j, 2) = WorksheetFunction.Percentile(ADatos, P)
j = j + 1
k = 0
FilaPrimerDato = i + 1
FilaUltimoDato = UF
Else
ADatos(k) = .Cells(i, 2)
k = k + 1
End If
Next
End With
End Sub