Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Changing the Cell Background Color when a keyword is entered

I'm trying to write a macro to change the cell background color when keywords are entered, something like

IF keyword = "north" THEN cell.Font.Color = Blu
IF keyword = "south" THEN cell.Font.Color = Re
IF keyword = "east" THEN cell.Font.Color = Gree
IF keyword = "west" THEN cell.Font.Color = Orang
IF keyword = "central" THEN cell.Font.Color = Pin

There will definitely be more than 3 keywords so I will not be able to use Conditional Formatting

Any help would be appreciated. Thanks.

Rachael
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Changing the Cell Background Color when a keyword is entered

Rachael,

Here is some event code that will handle those values in column B (2).

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
On Error GoTo ws_exit
If Target.Column = 2 Then
Select Case LCase(Target.Value)
Case "north": Target.Font.ColorIndex = 5 'Blue
Case "south": Target.Font.ColorIndex = 3 'Red
Case "east": Target.Font.ColorIndex = 10 'Green
Case "west": Target.Font.ColorIndex = 46 'Orange
Case "central": Target.Font.ColorIndex = 7 'Pink
End Select
End If


ws_exit:
Application.EnableEvents = True

End Sub

Add to the worksheet code module.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rachael" wrote in message
...
I'm trying to write a macro to change the cell background color when

keywords are entered, something like:

IF keyword = "north" THEN cell.Font.Color = Blue
IF keyword = "south" THEN cell.Font.Color = Red
IF keyword = "east" THEN cell.Font.Color = Green
IF keyword = "west" THEN cell.Font.Color = Orange
IF keyword = "central" THEN cell.Font.Color = Pink

There will definitely be more than 3 keywords so I will not be able to use

Conditional Formatting.

Any help would be appreciated. Thanks.

Rachael



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Changing the Cell Background Color when a keyword is entered

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell as Range
Dim Keyword as String
If Target.count 1 then exit sub
If target.Column = 3 then
Keyword = lcase(target.value)
Set cell = Target
IF keyword = "north" THEN cell.Font.ColorIndex = 5
IF keyword = "south" THEN cell.Font.ColorIndex = 3
IF keyword = "east" THEN cell.Font.ColorIndex = 4
IF keyword = "west" THEN cell.Font.ColorIndex = 45
IF keyword = "central" THEN cell.Font.ColorIndex = 40
End if
End Sub

Right click on the sheet tab and select view code.

Paste in code like the above. the target.column = 3 limits the action to
changes made in column 3. Change to adjust

--
Regards,
Tom Ogilvy


"Rachael" wrote in message
...
I'm trying to write a macro to change the cell background color when

keywords are entered, something like:

IF keyword = "north" THEN cell.Font.Color = Blue
IF keyword = "south" THEN cell.Font.Color = Red
IF keyword = "east" THEN cell.Font.Color = Green
IF keyword = "west" THEN cell.Font.Color = Orange
IF keyword = "central" THEN cell.Font.Color = Pink

There will definitely be more than 3 keywords so I will not be able to use

Conditional Formatting.

Any help would be appreciated. Thanks.

Rachael



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Changing the Cell Background Color when a keyword is entered

Thank you Bob & To

How do I change the background color rather than the font color

Rachae



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Changing the Cell Background Color when a keyword is entered

Rachael,

Try Interior.Colorindex rather than Font.Colorindex.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rachael" wrote in message
...
Thank you Bob & Tom

How do I change the background color rather than the font color?

Rachael








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Changing the Cell Background Color when a keyword is entered

cell.Interior.ColorIndex =

--
Regards,
Tom Ogilvy

"Rachael" wrote in message
...
Thank you Bob & Tom

How do I change the background color rather than the font color?

Rachael






  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Changing the Cell Background Color when a keyword is entered

Rachael,

Use the Worksheet_Change event procedure. For example, put the
following code in the appropriate worksheet's code module:


Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
For Each Rng In Target.Cells
Select Case Rng.Text
Case "north"
Rng.Font.ColorIndex = 5
Case "south"
Rng.Font.ColorIndex = 3
' and so on
End Select
Next Rng
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com

"Rachael" wrote in message
...
I'm trying to write a macro to change the cell background color

when keywords are entered, something like:

IF keyword = "north" THEN cell.Font.Color = Blue
IF keyword = "south" THEN cell.Font.Color = Red
IF keyword = "east" THEN cell.Font.Color = Green
IF keyword = "west" THEN cell.Font.Color = Orange
IF keyword = "central" THEN cell.Font.Color = Pink

There will definitely be more than 3 keywords so I will not be

able to use Conditional Formatting.

Any help would be appreciated. Thanks.

Rachael



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Changing the Cell Background Color when a keyword is entered

Thanks very much everyone x
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
Changing Cell Background Color based on data from another cell Speedy Excel Discussion (Misc queries) 2 March 16th 09 04:10 PM
changing background color Numa Excel Discussion (Misc queries) 0 June 10th 08 04:36 PM
Changing Cell Background Color Depending on the Number Matlock Excel Discussion (Misc queries) 1 February 18th 08 12:41 AM
changing cell background color okelbes Excel Worksheet Functions 1 September 20th 06 03:28 PM
Changing background color based on different cell djarcadian Excel Discussion (Misc queries) 3 August 10th 06 10:44 PM


All times are GMT +1. The time now is 04:30 AM.

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"