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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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