Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Assign a color to the cell

I would like to get colors to the different cell whenever I enter a
specific number. Such as all 1=Yellow and I tried conditional
formatting but I need at least 12 number and color. Can I do it with
VBA?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Assign a color to the cell

Hi

You can try the following you just have to repeat the statement and
replace the X with the number you want
and the XX with the colorindex you need for the number.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target = 1 Then
Target.Interior.ColorIndex = XX

ElseIf Target = X Then
Target.Interior.ColorIndex = XX
' repeat the above two lines as many times as needed

End If

End Sub

I hope this helps

Christian

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Assign a color to the cell

Hi Tr2USA,

Try something like:

'=============
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, rng2 As Range
Dim rcell As Range

Set rng = Me.Range("A1:A10") '<<==== CHANGE
Set rng2 = Intersect(rng, Target)

If Not rng2 Is Nothing Then
For Each rcell In rng2.Cells
With rcell
Select Case .Value
Case 1: .Interior.ColorIndex = 3
Case 2: .Interior.ColorIndex = 4
Case 3: .Interior.ColorIndex = 5
Case 4: .Interior.ColorIndex = 6
Case 5: .Interior.ColorIndex = 7
Case 6: .Interior.ColorIndex = 8
Case 7: .Interior.ColorIndex = 9
Case 8: .Interior.ColorIndex = 10
Case 9: .Interior.ColorIndex = 11
Case 10: .Interior.ColorIndex = 12
Case 11: .Interior.ColorIndex = 13
Case 12: .Interior.ColorIndex = 14
Case Else: .Interior.ColorIndex = xlNone
End Select
End With
Next rcell
End If

End Sub
'<<=============

Change the address of the rng variable to accord with the range of interest
and amend the ColorIndex values to taste.

This is worksheet event code and should be pasted into the worksheets's code
module (not a standard module and not the workbook's ThisWorkbook module):

Right-click the worksheet's tab
Select 'View Code' from the menu and paste the code.
Alt-F11 to return to Excel.


---
Regards,
Norman


wrote in message
oups.com...
I would like to get colors to the different cell whenever I enter a
specific number. Such as all 1=Yellow and I tried conditional
formatting but I need at least 12 number and color. Can I do it with
VBA?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Assign a color to the cell

This is great work. Thanks.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Assign a color to the cell

Thank you for your prompt answer. Very helpful.

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
Assign Color to a Cell based on another Cell on a diff Sheet VROSARIO7 Excel Worksheet Functions 2 July 6th 09 08:53 PM
assign button for color bk Excel Worksheet Functions 9 April 8th 07 04:04 PM
Can you assign specific color to cell using (if-then) functions Ex Huskerz Excel Worksheet Functions 1 November 28th 06 10:15 PM
Charts: want to assign fixed color for each slice [email protected] Excel Discussion (Misc queries) 1 November 15th 05 07:49 PM
Assign a color to a cell Junkyard Engineer Excel Worksheet Functions 1 June 6th 05 08:29 PM


All times are GMT +1. The time now is 11:45 AM.

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

About Us

"It's about Microsoft Excel"