Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 473
Default Formatting Cells Dependent on Contents

Danny wrote:
Is their an easy piece of code which could be used on an
entire worksheet to format the cells dependent on their
content. I.e if a cell contains the text 'house' then the
cell has a red background etc. for about 50 conditions.


First you need a procedure to do the colouring.
Maybe something like this:

Sub ColourCells(R As Range)
Dim C As Range
For Each C In R.Cells
Select Case TypeName(C.Value)
Case "Error"
C.Interior.ColorIndex = 1 ' black!
Case "Empty"
C.Interior.ColorIndex = xlNone
Case "Double", "Boolean", "Date"
' a number etc.
C.Interior.ColorIndex = xlNone
Case "String"
Select Case C.Value
Case "House"
C.Interior.ColorIndex = 3 ' red
Case "Car"
C.Interior.ColorIndex = 4 ' red
'etc.
Case Else
C.Interior.ColorIndex = xlNone
End Select
End Select
End Sub

You could test it by selecting some cells and running macro Test:

Sub Test()
ColourCells Selection
End Sub

Then in the module behind the worksheet concerned

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
ColourCells Target
End Sub

This assumes that none of the cells to be coloured are a result of
formulas - if they are then you also need:

Private Sub Worksheet_Calculate()
ColourCells Me.UsedRange.SpecialCells(xlFormulas)
End Sub


Bill Manville
MVP - Microsoft Excel, Oxford, England
No email replies please - reply in newsgroup

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional formatting using part of the cells contents Duey Excel Discussion (Misc queries) 0 April 27th 10 06:33 PM
Directing cell contents to one of two cells dependent on its value Debi Excel Worksheet Functions 1 April 15th 10 03:13 PM
directing cell contents dependent on its value Debi Excel Worksheet Functions 2 April 15th 10 09:58 AM
how do I fix formatting mistakes: cells and contents are wavy superteacher Excel Worksheet Functions 0 March 25th 09 03:10 AM
Data Validation list dependent on contents of another cell Scorer150 Excel Discussion (Misc queries) 2 February 7th 08 12:13 AM


All times are GMT +1. The time now is 10:28 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"