Excel/VBA

Advertisement


Question
Evening,
I'm trying to circumvent the conditional format limit of 3 in Excel 03.   I have a file with various percents and want to color code each percent threshold.   For instance, if it is 60% to 75% color code it 27 (excel color code).  Don't know how to get VBA to read the formated percent that I have in the Excel file and I can't seem to get the proper verbiage for a range (60 to 70...) ~ hair loss is imminent.   This is what I have, but it is not working.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim icolor As Integer

Set MyPage = Range("$e$2:$bn$63")
   For Each Cell In MyPage
   
       If Cell.Value = 0 Then
           Cell.Interior.ColorIndex = 15
       End if
       If Cell.Value > 76 < 89 Then
           Cell.Interior.ColorIndex = 35
       End if
       If Cell.Value > 89 Then
           Cell.Interior.ColorIndex = 43
       End if
       If Cell.Value > 60 < 75 Then
           Cell.Interior.ColorIndex = 27
       End if
       If Cell.Value > 50 < 60 Then
           Cell.Interior.ColorIndex = 45
       End if
       If Cell.Value > 1 < 50 Then
           Cell.Interior.ColorIndex = 45
       End If

  Next
End sub
--
Regards ~ David


Answer
David,

percent values are stored as decimal values if the cell actually holds a number and you have it formatted to display percent (rather than having the string "80%").  80% is stored as .80

You can see this by selecting the cell and formatting it as general.  

so in  your could you would do

Private Sub Worksheet_Change(ByVal Target As Range)

Dim icolor As Integer

Set MyPage = Range("$e$2:$bn$63")
  For Each Cell In MyPage
  
      If Cell.Value = 0 Then
          Cell.Interior.ColorIndex = 15
      End if
      If Cell.Value >= .76 and cell.value  < .89 Then
          Cell.Interior.ColorIndex = 35
      End if
      If Cell.Value >= .89 Then
          Cell.Interior.ColorIndex = 43
      End if
      If Cell.Value >= .60 and cell.Value < .75 Then
          Cell.Interior.ColorIndex = 27
      End if
      If Cell.Value >= .50 and cell.value < .60 Then
          Cell.Interior.ColorIndex = 45
      End if
      If Cell.Value >= .01 and cell value < .50 Then
          Cell.Interior.ColorIndex = 45
      End If

 Next
End sub


--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2012 About.com, a part of The New York Times Company. All rights reserved.