PDA

View Full Version : add an addition cell colour to coding



peter renton
11-14-2014, 08:30 PM
Hi

Could anybody tell me how to add an additional colour to this code
at the moment if text is entered with N-SIDE all the related cells will go yellow
I have been trying to add it the text is ADD then they should turn pink.

This is probably simple but not to me







Sub shadeCells(rangeToShade As Range, Target_Name As String)
Dim Shaded(1 To 9) As Long, i As Integer, j As Integer
Dim myColor As Long

If UCase(Left(Target_Name, 6)) = "N-SIDE" Then
myColor = 65535
Else
myColor = 4050606
End If

'Set the shading color numbers for each column
Shaded(1) = myColor 'Col A
Shaded(2) = myColor 'Col B
Shaded(3) = myColor 'Col C
Shaded(4) = myColor 'Col D
Shaded(5) = myColor 'Col E
Shaded(6) = myColor 'Col F
Shaded(7) = myColor 'Col G
Shaded(8) = myColor 'Col H
Shaded(9) = myColor 'Col I

For j = 1 To rangeToShade.Rows.Count
For i = 1 To 9
Cells(rangeToShade.Rows(j).Row, i).Interior.Color = Shaded(i)
Next
Next
End Sub



Thank you for looking

Peter

Admin
11-15-2014, 12:39 PM
If UCase(Left(Target_Name, 6)) = "N-SIDE" Then
myColor = 65535
ElseIf UCase(Left(Target_Name, 3)) = "ADD" Then
myColor = 13353215 'Pink
Else
myColor = 4050606
End If

p45cal
11-20-2014, 05:16 PM
Since there is only one colour, you don't need to set up arrays, nor colour the cells one by one, and if you want to add more colours later a select case construct is easier to add to:
Sub shadeCells(rangeToShade As Range, Target_Name As String)
Dim myColor As Long
Select Case True
Case UCase(Left(Target_Name, 6)) = "N-SIDE": myColor = 65535
Case UCase(Left(Target_Name, 3)) = "ADD": myColor = 13353215
'Case UCase(Left(Target_Name, 14)) = "SOMETHING ELSE": myColor = 9999' add more if you want.
Case Else: myColor = 4050606
End Select
rangeToShade.Resize(, 9).Interior.Color = myColor
End Sub