Help with Calculate the percentiles by VBA

nhnn1986

Board Regular
Joined
Oct 12, 2017
Messages
92
I want to Calculate the percentiles by VBA, code belove wrong thiss line:

If percentileArray(1) > 0 Then
Set group1 = dataSheet.Range("N1").Resize(1, percentileArray(1))
Else

Please help me
Full code
VBA Code:
Sub SplitDataByPercentile()

    ' Set the range of data to be split
    Dim dataSheet As Worksheet
    Set dataSheet = ThisWorkbook.Sheets("KHOI_NHTM")
    Dim dataRange As Range
    Set dataRange = dataSheet.Range("N2", dataSheet.Cells(2, dataSheet.Columns.Count).End(xlToLeft))
    
    ' Check if dataRange has enough data
    If dataRange.Columns.Count < 3 Then
        MsgBox "Unable to calculate percentiles. Please check if there is enough data."
        Exit Sub
    End If
    
    ' Calculate the percentiles
    Dim percentileArray As Variant
    percentileArray = Application.Percentile(dataRange, Array(0.33, 0.67))
    
    ' Check if percentileArray has values
    If UBound(percentileArray) < 1 Then
        MsgBox "Unable to calculate percentiles. Please check if there is enough data."
        Exit Sub
    End If
    
    ' Split the data into three groups
    Dim group1 As Range, group2 As Range, group3 As Range
    
    ' Check if percentileArray has values for group1
    If percentileArray(1) > 0 Then
        Set group1 = dataSheet.Range("N1").Resize(1, percentileArray(1))
    Else
        MsgBox "Unable to split data into groups. Please check if there is enough data."
        Exit Sub
    End If
    
    ' Check if percentileArray has values for group2
    If percentileArray(1) >= 0 And percentileArray(2) > percentileArray(1) Then
        Set group2 = dataSheet.Range("N1").Offset(0, percentileArray(1)).Resize(1, percentileArray(2) - percentileArray(1))
    Else
        MsgBox "Unable to split data into groups. Please check if there is enough data."
        Exit Sub
    End If
    
    ' Check if percentileArray has values for group3
    If percentileArray(2) >= percentileArray(1) And percentileArray(2) < dataRange.Columns.Count Then
        Set group3 = dataSheet.Range("N1").Offset(0, percentileArray(2)).Resize(1, dataRange.Columns.Count - percentileArray(2))
    Else
        MsgBox "Unable to split data into groups. Please check if there is enough data."
        Exit Sub
    End If
    
    ' Check if group1 has values
    If group1.Columns.Count = 0 Then
        MsgBox "Unable to split data into groups. Please check if there is enough data."
        Exit Sub
    End If
    
    ' Check if the "Peer_group" sheet exists and delete it if it does
    Dim peerGroupSheet As Worksheet
    On Error Resume Next
    Set peerGroupSheet = ThisWorkbook.Sheets("Peer_group")
    On Error GoTo 0
    If Not peerGroupSheet Is Nothing Then
        Application.DisplayAlerts = False
        peerGroupSheet.Delete
        Application.DisplayAlerts = True
    End If
    
    ' Create a new sheet named "Peer_group"
    Set peerGroupSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    peerGroupSheet.Name = "Peer_group"
    
    ' Check if dataRange has enough data to copy
    If dataRange.Rows.Count < 1 Then
        MsgBox "Unable to copy data to the new sheet. Please check if there is enough data."
        Exit Sub
    End If
    
    ' Copy the data and headers to the new sheet
    dataRange.Copy peerGroupSheet.Range("N2")
    dataSheet.Range("N1", dataSheet.Cells(1, dataSheet.Columns.Count).End(xlToLeft)).Copy peerGroupSheet.Range("N1")
    
    ' Add borders to the groups
    group1.BorderAround xlContinuous, xlMedium
    group2.BorderAround xlContinuous, xlMedium
    group3.BorderAround xlContinuous, xlMedium
    
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If percentileArray(1) > 0 Then
Set group1 = dataSheet.Range("N1").Resize(1, percentileArray(1))

Specifically, which of the two lines above causes the error?

And, which error does it cause?
 
Upvote 0
Which error number and message does it display when the error occurs?

When the error occurs and you click on Debug so that it brings you to that line, enter the following line into the Immediate Window (Visual Basic Editor >> View >> Immediate Window ) and press ENTER...

VBA Code:
? percentileArray(1)

What does it return?
 
Upvote 0
Which error number and message does it display when the error occurs?

When the error occurs and you click on Debug so that it brings you to that line, enter the following line into the Immediate Window (Visual Basic Editor >> View >> Immediate Window ) and press ENTER...

VBA Code:
? percentileArray(1)

What does it return?
when I run code" application error or object defined error
 
Upvote 0
Okay, so it looks like for some reason Percentile is returning an error value.

Does the range dataRange contain error values, such as #VALUE!, #N/A, etc?
 
Upvote 0
Or may the range is empty?

Or maybe the range being define by dataRange is not the expected range?
 
Upvote 0
My data like this, sheets ("KHOI_NHTM")

I want 03 group by VBA with percentile

CTCTCONFIDENCESUMMAXMINAverageSTDEV.P56201001-20251256202001-20251256202002-20251256203001-20251256203002-20251256204001-20251256302001-20251256303001-20251256303002-20251256304001-20251256305001-20251256306001-20251256307001-20251256309001-20251256310001-20251256311001-20251256311002-20251256313001-20251256314001-20251256317001-20251256319001-20251256321001-20251256323001-20251256327001-20251256333001-20251256334001-20251256339001-20251256341001-20251256348001-20251256353001-20251256356001-20251256357001-20251256358001-20251256359001-20251256360001-20251256505001-20251256207001-202512
CT0000data
1.276.825​
146.127.598​
17.574.639​
326.898​
3.949.395​
3.962.637​
8.997.367​
10.652.993​
6.866.551​
16.055.704​
5.660.755​
17.574.639​
1.704.023​
8.553.221​
2.053.505​
326.898​
1.593.059​
5.657.418​
4.281.649​
648.956​
2.192.628​
5.270.508​
2.056.274​
551.584​
1.748.439​
2.928.631​
726.764​
3.543.556​
1.198.936​
1.264.142​
2.812.153​
4.198.008​
1.580.192​
1.519.657​
6.733.209​
3.375.701​
2.051.105​
1.554.061​
1.711.741​
1.134.886​
2.046.208​
1.403.988​
3.898.491​
 
Upvote 0
It looks like the numbers in your range are being recognized as text values, not numerical values. And so Percentile returns an error.

First, I'm assuming that your version of Excel uses the decimal point ( . ) instead of a comma ( , ) as the thousand separator . If not, you'll need to remove them, and then format your numbers as desired.

Secondly, when I copy and paste the data into an Excel worksheet, I noticed that there's an invisible character at the end of each number. The invisible character is a zero width space. You can confirm this by entering the following formula in a cell...

Excel Formula:
=UNICODE(RIGHT(N2,1))

The formula should return the UNICODE decimal value 8203, which is equivalent to the hexadecimal value 0x200B. With this information, you can remove this character by using the following formula...

Excel Formula:
=SUBSTITUTE(N2,UNICHAR(8203),"")+1

Although, if your version of Excel in fact uses a comma as a thousand separator, you'll need to remove the decimal point in addition to the invisible character...

Excel Formula:
=SUBSTITUTE(SUBSTITUTE(N2,UNICHAR(8203),""),".","")+1

And so you can remove those invisible characters from each cell as follows...

1) Make sure that the cells containing your numbers are formatted to display as one of the number formats. So select N2:AX2, and format as a number (Ribbon >> Home tab >> Number group >> General, Number, etc).

2) Enter the following formula in N3, and copy across to AX3...

Excel Formula:
=SUBSTITUTE(N2,UNICHAR(8203),"")+1

3) Select N3:AX3, and click on Copy.

4) Select N2:AX2, and paste as values.

5) Delete Row 3.

Hope this helps!
 
Upvote 0
By the way, since Application.Percentile(dataRange, Array(0.33, 0.67)) returns a two-element array containing either numbers or error values, If UBound(percentileArray) < 1 Then will always evaluate to False. Try the following instead...

VBA Code:
If Application.Count(percentileArray) <> 2 Then

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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