VBA Code to Write Percentile Function

nirvehex

Well-known Member
Joined
Jul 27, 2011
Messages
505
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to write some VBA code that does the same thing as this Excel Code:
Code:
 {=PERCENTILE.INC(IF($A$2:LASTROW=A2,$AF$2:LASTROW),.5)}

I know of the function: [code]Application.WorksheetFunction.Percentile(
but not sure how to use it.

Can anyone help me out please?

Thanks!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Nirvehex,

something in this direction should work:
Code:
ActiveSheet.Evaluate = "=PERCENTILE.INC(IF($A$2:LASTROW=A2,$AF$2:LASTROW),.5)"
An alternative: try the macro recorder when you CTRL+SHIFT+ENTER your function and see what is recorded.
Cheers,

Koen
 
Upvote 0
Hi Thanks Rinjnsent. I tried ActiveSheet.Evaluate. Unfortunately that didn't work. When I do the macro recorder, I get Row Column (RC) formatting which won't work either.

I feel like my code is so close. Here's what I have:

Code:
Sheets("WOs (DSP Only)").Select


    Range("A2").Select
    ActiveCell.Formula = "=CONCATENATE(L3," - ",K3," - ",Z3," - ",X3)"
    Selection.AutoFill Destination:=Range("A2:A & LastRow")
    
    Range("B2").Select
     ActiveCell.FormulaArray = "=PERCENTILE.INC(IF($A$2:$A$" & Lastrow & "=A2,$AF$2:$A$" & Lastrow & "),.5)"
     Selection.AutoFill Destination:=Range("B2:B & LastRow")

Any other thoughts?

Thanks!
-Mark
 
Upvote 0
You have a number of issues regarding the double quotes. Also, there's no need to use the Select method. Try the following instead...

Code:
    With Sheets("WOs (DSP Only)")
        .Range("A2").Formula = "=CONCATENATE(L3,"" - "",K3,"" - "",Z3,"" - "",X3)"
        .Range("A2").AutoFill Destination:=.Range("A2:A" & LastRow)
        .Range("B2").FormulaArray = "=PERCENTILE.INC(IF($A$2:$A$" & LastRow & "=A2,$AF$2:$AF$" & LastRow & "),.5)"
        .Range("B2").AutoFill Destination:=.Range("B2:B" & LastRow)
    End With

And, of course, you can always select/activate your sheet at the end, if so desired.

Hope this helps!

P.S. Note that I corrected the second range being referenced in your array formula to what I believe should be $AF$2:$AF. Hopefully, this was your intent. If not, please ignore the change.
 
Last edited:
Upvote 0
Thanks Domenic and nice catch on my typo. This code is so much cleaner and worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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