ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to use VB for multiple conditions (https://www.excelbanter.com/excel-programming/422773-re-how-use-vbulletin-multiple-conditions.html)

Dave Peterson

How to use VB for multiple conditions
 
If you want to assign the colors yourself:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myVals As Variant
Dim myColors As Variant
Dim myColor As Long
Dim Res As Variant

myVals = Array("V", "F", "A", "OC", "CO", _
"O1", "O2", "O3", "O4", _
"O5", "O6", "O7", "O8")

myColors = Array(1, 3, 5, 6, 7, _
12, 2, 13, 22, _
34, 37, 44, 58)

If UBound(myVals) = UBound(myColors) Then
'ok, keep going
Else
MsgBox "Design error!"
Exit Sub
End If

If Target.Cells.Count 1 Then
Exit Sub 'one cell at a time
End If

If Intersect(Target, Me.Range("a:a")) Is Nothing Then
Exit Sub 'only in column A
End If

Res = Application.Match(Target.Value, myVals, 0)
If IsNumeric(Res) Then
myColor = myColors(Res - 1)
Else
myColor = xlNone
End If

Target.Interior.ColorIndex = myColor

End Sub

When I do this, I record a macro when I change the fill color. Then I look at
the code to see what number that color represented.

OsmoseTom wrote:

I need some help. I need to change the color of a cell based on values from
a list. The list options are V, F, A, OC, CO, O1, O2, O3, O4, O5, O6, O7, &
O8. Each option would have to be a different color. Can this be done using
VB code for the current worksheet? If so how? Thanks...


--

Dave Peterson


All times are GMT +1. The time now is 04:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com