I made a macro, that changes the color of specific characters in a range of cells.
The macro works if the cell content is written manually.
I want the macro to read the formula result in the range instead, because the cells will have different combinations of x, y and 7 according to a user defined function (a bunch of if-statements).
The macro is showing wrong colors, when the cell content is not manually written.
I tried a time delay for my macro, so it would execute after my user defined function, because I thought my macro maybe executed before. That didn't work. That's why I think the problem is that the macro doesn't read the formula result.
Public Sub ChangeColor()
Dim MyRange As Range
Dim FarveZ As Integer
Dim FarveX As Integer
Dim Farve7 As Integer
Set MyRange = Range("G32:R34")
FarveZ = 26
FarveX = 46
Farve7 = 3
For Each tempstring In MyRange
If tempstring = "zx7" Then
tempstring.Characters(Start:=1, Length:=1).Font.ColorIndex = FarveZ
tempstring.Characters(Start:=2, Length:=1).Font.ColorIndex = FarveX
tempstring.Characters(Start:=3, Length:=1).Font.ColorIndex = Farve7
ElseIf tempstring = "zx" Then
tempstring.Characters(Start:=1, Length:=1).Font.ColorIndex = FarveZ
tempstring.Characters(Start:=2, Length:=1).Font.ColorIndex = FarveX
ElseIf tempstring = "z7" Then
tempstring.Characters(Start:=1, Length:=1).Font.ColorIndex = FarveZ
tempstring.Characters(Start:=2, Length:=1).Font.ColorIndex = Farve7
ElseIf tempstring = "x7" Then
tempstring.Characters(Start:=1, Length:=1).Font.ColorIndex = FarveX
tempstring.Characters(Start:=2, Length:=1).Font.ColorIndex = Farve7
ElseIf tempstring = "z" Then
tempstring.Characters(Start:=1, Length:=1).Font.ColorIndex = FarveZ
ElseIf tempstring = "x" Then
tempstring.Characters(Start:=1, Length:=1).Font.ColorIndex = FarveX
ElseIf tempstring = "7" Then
tempstring.Characters(Start:=1, Length:=1).Font.ColorIndex = Farve7
Else
Exit Sub
End If
Next tempstring
End Sub
I don't get any error messages. It just doesn't come up with the right colors.
tempstringis not a string, but a range, the name is quite misleading IMO. BTWtempstringdoesn't seem declared, is it somewhere else in the code? - Vincent GIf... Then ... End Ifblock by aSelect Casestructure, it will probably be more readable. And use tempstring.Value2 to evaluate the value. - Vincent GOption Explicitat the start of any module. This will force you to declare all variables. - Zack E