Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / March 2008

Tip: Looking for answers? Try searching our database.

macro to color top 10% in range

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
G.R. - 28 Feb 2008 07:49 GMT
Hello.  
I'm using Excel X (VBA5) and trying write a macro which will change the
interior cell color of those cells whose value is in the top ten percent of
the selected range of cells. I was able to do this quite easily with
conditional formatting using the percentile formula, but I want to be able to
do it with a macro and explicit formatting. The below macro returns a "sub or
function not defined" error when it gets to PERCENTILE:

Sub NLFI_TopTenPercent()
'
' having selected the cells before running,
' first create range from selection
'
   Selection.Name = "TopTenPercent_Range"
'
' add Loop to check each cell in range
'
   For Each Cell In Range("TopTenPercent_Range")
'
' check Cell value against values in Range
' shade cell Yellow if > or =
'
       If (Cell.Value >= Percentile("TopTenPercent_Range", 0.9)) Then
           Cell.Interior.Color = vbYellow
       End If
   Next Cell
End Sub

Maybe I need a dim statement at the top?  
And and all help would be appreciated.
OssieMac - 28 Feb 2008 09:12 GMT
Percentile is a worksheet function.

If (Cell.Value >= WorksheetFunction.Percentile("TopTenPercent_Range", 0.9))
Then

Signature

Regards,

OssieMac

> Hello.  
> I'm using Excel X (VBA5) and trying write a macro which will change the
[quoted text clipped - 26 lines]
> Maybe I need a dim statement at the top?  
> And and all help would be appreciated.
G.R. - 29 Feb 2008 21:28 GMT
OssieMac -
Thank you very much for your reply.  I tried it and got the following error:

"Method 'Percentile' of object 'WorksheetFunction' failed"

Any ideas?
OssieMac - 29 Feb 2008 22:09 GMT
Hi again,

There was a syntax error. I tested the following and it works fine. Need to
specify range for the named range.

Sub NLFI_TopTenPercent()
Range("A1:A1000").Select
Selection.Name = "TopTenPercent_Range"
For Each Cell In Range("TopTenPercent_Range")
   If (Cell.Value >=
WorksheetFunction.Percentile(Range("TopTenPercent_Range"), 0.9)) Then
       Cell.Interior.Color = vbYellow
   End If
Next Cell
End Sub

Signature

Regards,

OssieMac

> OssieMac -
> Thank you very much for your reply.  I tried it and got the following error:
>
> "Method 'Percentile' of object 'WorksheetFunction' failed"
>
> Any ideas?
G.R. - 01 Mar 2008 21:34 GMT
OssieMac,
Brilliant!  Worked perfectly.  Thank you again for your time and attention.  
I'm very grateful.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.