I've written a function that essentially returns a weighted score by assigning values of 1 and 2 to different conditionally formatted cells. When I call this function in the worksheet, it returns the #VALUE! error.
Function ColorWeight(Factors As Range) As Integer
Dim x As Range
Dim score As Integer
score = 0
For Each x In Factors
If x.DisplayFormat.Interior.color = Range("C5").Interior.color Then
score = score + Cells(14, x.Column).Value * 2
ElseIf x.DisplayFormat.Interior.color = Range("C9").Interior.color Then
score = score + Cells(14, x.Column).Value * 1
End If
Next
ColorWeight = score
End Function
However, when I run this code as a sub and set the range to a specific range, as below, it works fine.
Sub ColorWeight()
Dim Factors As Range
Dim x As Range
Dim score As Integer
Set Factors = Range("G17:Q17")
score = 0
For Each x In Factors
If x.DisplayFormat.Interior.color = Range("C5").Interior.color Then
score = score + Cells(14, x.Column).Value * 2
ElseIf x.DisplayFormat.Interior.color = Range("C9").Interior.color Then
score = score + Cells(14, x.Column).Value * 1
End If
Next
Debug.Print score
End Sub
What is the difference that I'm missing that makes the function not work?