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
 
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!
Thnaks for your help
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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