Re: Writing Udf For If Color Then Else
Code
Function SumIfPurple(inputRange As Range, _
answerRange1 As Range, _
answerRange2 As Range) As Variant
Dim SumAnswer As Variant
If Range.Interior.ColorIndex = 39 Then
SumAnswer = answerRange1.Value
Else
SumAnswer = answerRange2.Value
End If
SumIfPurple = SumAnswer
End Function
Display More
I made a few changes to your code.
1) Range is a reserved term in VB. It should not be used as a variable name, Range1 and Range2 are too close for my comfort so I changed those too.
2) Declaring all data types speeds things up and helps keep you straight as to what you are doing.
2a) Similarly, although usualy unnessesary, using the .Value property helps Excel things straight.
3) The big problem was in the Range1=SumAnswer statement. It was wrong side around.
I hope this helps.