We help IT Professionals succeed at work.

Excel - Find if a value is in the top 25 %, 50% or 75% of the total value

cd_morris
cd_morris asked
on
716 Views
Last Modified: 2012-06-27
Is it possible with VBA
1. get the sum of column 3,
2. and then in column 4 determine if value in column 3 is
- in the top1-25% range (If so put an A in the cell)
- in the 26- 50% range (If so put an B in the cell)
- in the 51- 75-% range (If so put an C in the cell)
- orin the bottom 25% (If so put an D in the cell)
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
Assuming you mean you have a series of numbers in Col 3, and you want to "bucketize" them by quartile...
Sub DoTheMath()
 
    Dim LastR As Long
 
    With ActiveSheet
        LastR = .Cells(.Rows.Count, "c").End(xlUp).Row
        .Range("d2:d" & LastR).Formula = "=VLOOKUP(PERCENTRANK(C2:C" & LastR & _
            ",C2),{0,""D"";0.2500001,""C"";0.5000001,""B"";0.7500001,""A""},2)"
        .Range("b" & (LastR + 2)) = "Total"
        .Range("c" & (LastR + 2)).Formula = "=SUM(C2:C" & LastR & ")"
    End With
 
End Sub

Open in new window

byundtMechanical Engineer
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
Assuming all numbers are positive, and without VBA, you might use the formula:
=CHOOSE(1+4*C2/SUM(C$2:C$100),"D","C","B","A")

The equivalent macro is shown below.

Brad
Sub Ranker()
Dim dSum As Double
Dim rg As Range
Set rg = Range("C2") 'First cell in range to be summed
Set rg = Range(rg, Cells(Rows.Count, rg.Column).End(xlUp)) 'All the cells with data
dSum = Application.Sum(rg)
rg.Offset(0, 1).FormulaR1C1 = "=CHOOSE(1+4*RC[-1]/" & dSum & ",""D"",""C"",""B"",""A"")"
rg.Offset(0, 1).Formula = rg.Offset(0, 1).Value
End Sub

Open in new window

CERTIFIED EXPERT
Top Expert 2010

Commented:
Brad,I guess it all turns on what those percent values are supposed to mean.  I took them as meaning percentiles, but who knows... :)Patrick

Author

Commented:
Mathew:
Your solution gave m mostly A's a few C' and D's but no C's.

Byund:
Your solution gave me all D's
RankData.xls
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.