Advertisement

01.12.2008 at 10:16PM PST, ID: 23078756
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Color Code rows based on a cell value in Excel 2007

Tags: VBA
I have a spreadsheet that we download once a year from a source and then we go through and color code the sheet.  I would like to develop code that based on a value in a cell at the end of the row the row will be colored a defined a color.  

 For example if I place the letter "N" in a cell it will result in red and if it is "L" the color is green.  I would use conditional formatting but I will need to be able to define ten colors.  So the code would need to be able to define the color and associate it with a letter and color the row based on the changed value in the cell.   The value will always show up in the same column.  Can any one give me some ideas of what this code might look like?
Start your free trial to view this solution
Question Stats
Zone: Software
Question Asked By: Apex623
Solution Provided By: byundt
Participating Experts: 4
Solution Grade: A
Views: 454
Translate:
Loading Advertisement...
01.12.2008 at 10:31PM PST, ID: 20646660

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.12.2008 at 11:28PM PST, ID: 20646797

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2008 at 12:25AM PST, ID: 20646967

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2008 at 09:06AM PST, ID: 20648228

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2008 at 05:25PM PST, ID: 20650132

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.18.2008 at 10:28PM PST, ID: 20696410

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.18.2008 at 11:00PM PST, ID: 20696457

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.12.2008 at 10:31PM PST, ID: 20646660

Rank: Genius

Hi Apex623,

You can use code similar to this in the worksheets code module (right-click tab > View Code)....

    Private Sub Worksheet_Change(ByVal Target As Range)
        'ignore all column except column A
        If Intersect(Target, [A:A]) Is Nothing Then Exit Sub
        Select Case UCase(Target.Value)
            Case "L": Target.Interior.ColorIndex = 3    'red
            Case "D": Target.Interior.ColorIndex = 5    'blue
            Case "A": Target.Interior.ColorIndex = 4    'green
            Case "B": Target.Interior.ColorIndex = 6    'yellow
            Case "C": Target.Interior.ColorIndex = 44    'gold
        End Select
    End Sub

Regards,

Wayne
Assisted Solution
 
01.12.2008 at 11:28PM PST, ID: 20646797

Rank: Sage

I agree with Wayne with one comment, if you want the entire row colouring based on your reference cell then use the following syntax for the case statements:

            Case "L": Target.EntireRow.Interior.ColorIndex = 3    'red

The difference being the addition of the 'entirerow' qualifier

Chris
 
01.13.2008 at 12:25AM PST, ID: 20646967

Rank: Genius

If you are using Excel 2007 you do not have the old limit of 3 conditional formats to worry about so you can do it with conditional formatting if you want?
Regards,
Rory
 
01.13.2008 at 09:06AM PST, ID: 20648228

Rank: Genius

Since this is an annual deal, I suggest modifying Wayne's macro (with Rory's tweak) so it runs on the active worksheet and applies all the colors at once. See the RowColoring macro in the Code Snippet pane. Even though Excel 2007 supports more Conditional Formatting criteria, you would need to install all ten on the downloaded workbook every year. With the macro, you spend a little more time in the first year to debug it, then just run the code year after year.

You'll need to get the ColorIndex numbers to fine-tune the code. Excel 2000 includes a color chart referencing the ColorIndex numbers in the VBA on-line help. Alas, this useful bit of documentation is not available in either earlier or later versions of Excel. To overcome this omission, the following sub produces a color chart in your worksheet:

Sub myColors()
Dim i As Long
For i = 0 To 14
    Cells(i + 1, 1) = i
    Cells(i + 1, 1).Interior.ColorIndex = i
    Cells(i + 1, 2) = i + 14
    Cells(i + 1, 2).Interior.ColorIndex = i + 14
    Cells(i + 1, 3) = i + 28
    Cells(i + 1, 3).Interior.ColorIndex = i + 28
    Cells(i + 1, 4) = i + 42
    Cells(i + 1, 4).Interior.ColorIndex = i + 42
Next i
End Sub

David McRitchie has a chart showing the names, ColorIndex numbers and swatches at http://www.mvps.org/dmcritchie/excel/colors.htm

If you want to use "names" for colors, Kevin (zorvek) posted code to create Enum constants at http:/Q_21472445.html#14313363


To install a sub in a regular module sheet:
1) ALT + F11 to open the VBA Editor
2) Use the Insert...Module menu item to create a blank module sheet
3) Paste the suggested code in this module sheet
4) ALT + F11 to return to the spreadsheet

To run a sub or macro:
5) ALT + F8 to open the macro window
6) Select the macro
7) Click the "Run" button

To set macro security so all macros will run (equivalent to Low security on Excel 2000 through 2003), do the following:
1) Click the Office Button (top left corner of window)
2) Click the "Excel Options" button at the bottom of the resulting dialog
3)  Go to the Trust Center tab, then click the button for "Trust Center Settings"
4) Go to the Macro Settings tab, then choose the option button for "Enable all macros (not recommended; potentially dangerous code can run)"

Excel 2007 also allows you to designate certain folders as being Trusted. Any macros contained in workbooks in those folders will be enabled automatically. A good candidate for such designation is a folder that contains a library of workbooks you have personally vetted. I don't advise so designating your main working folder, however, because you just might put a workbook containing malicious code in there at some point.


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
Sub RowColoring()
'Applies different highlights to rows according to value in specified column
Dim cel As Range, rg As Range
Application.ScreenUpdating = False
Set rg = [N2]   'First cell in column that needs to be checked
Set rg = Range(rg, Cells(Rows.Count, rg.Column).End(xlUp))
For Each cel In rg.Cells
    Select Case UCase(cel.Value)
        Case "L"
            cel.EntireRow.Interior.ColorIndex = 3    'red
        Case "D"
            cel.EntireRow.Interior.ColorIndex = 5    'blue
        Case "A"
            cel.EntireRow.Interior.ColorIndex = 4    'green
        Case "B"
            cel.EntireRow.Interior.ColorIndex = 6    'yellow
        Case "C"
            cel.EntireRow.Interior.ColorIndex = 44    'gold
    End Select
Next
Application.ScreenUpdating = True
End Sub
Open in New Window
Accepted Solution
 
01.13.2008 at 05:25PM PST, ID: 20650132
Okay, let me get cracking on these and see if I can make them work.  I will get back as soon as I can.
 
01.18.2008 at 10:28PM PST, ID: 20696410
Follow up question:

Byundt,

I think the code that you have supplied will work out great I do have a question though.  Instead of highlighting the whole row how would I write code to end at a certain column?

Also what does ucase mean?
 
01.18.2008 at 11:00PM PST, ID: 20696457

Rank: Genius

ucase("SOME text")     'returns "SOME TEXT"     it  makes everything upper case

To apply color just to specified columns, I changed the code as shown below. It now uses the Intersect function to color just cells in specified columns.

It is worth noting that coloring a portion of the row takes more memory than coloring the entire row. Don't worry about this fine point if you have only a few hundred rows. It can make a big difference if you have fifty thousand of rows of data, however.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Sub RowColoring()
'Applies different highlights to rows according to value in specified column
Dim cel As Range, rg As Range, rgColor As Range
Dim iColor As Long
Application.ScreenUpdating = False
Set rg = [N2]   'First cell in column that needs to be checked
Set rgColor = [A:M] 'Apply highlight to these columns
Set rg = Range(rg, Cells(Rows.Count, rg.Column).End(xlUp))
For Each cel In rg.Cells
    Select Case UCase(cel.Value)
        Case "L"
            iColor = 3    'red
        Case "D"
            iColor = 5    'blue
        Case "A"
            iColor = 4    'green
        Case "B"
            iColor = 6    'yellow
        Case "C"
            iColor = 44    'gold
    End Select
Next
Intersect(cel.EntireRow, rgColor).Interior.ColorIndex = iColor
Application.ScreenUpdating = True
End Sub
Open in New Window
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628