View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Update sheet after changed Interior.ColorIndex

Hi Fredrik,

One way to update such a udf might be to replace the formula in all cells
that contain the udf, eg

Sub UpDateUDF()
Dim sFirst As String
Dim cel As Range

On Error Resume Next
With ActiveSheet.UsedRange
Set cel = .Find(What:="FillColor", After:=.Cells(1, 1), _
LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False) _

If Not cel Is Nothing Then
cel.Formula = cel.Formula
sFirst = cel.Address
Do
Set cel = .FindNext(cel)
cel.Formula = cel.Formula
Loop While Not cel Is Nothing And cel.Address < sFirst
End If
End With

End Sub

In passing, couple of comments about your UDF. It's generally recommended
not to use a keyword like "Cell" as a variable name. A udf can only return a
value, so "Application.Calculate" or anything else that tries to change the
app or workbook does nothing.
Might be worth including an error handler:
On Error GoTo errH
'code
Exit Function
errH:
FillColor = CVErr(xlErrref)
End function

Bob - another of those early morning colour questions :-)

Regards,
Peter T


"FredrikLyhagen" wrote in message
oups.com...
Function FillColor(Cell As Range) As String
Dim C As Long
C = Cell.Interior.ColorIndex
If C = 1 Then
FillColor = "Black"
ElseIf C = 9 Then
FillColor = "Dark Red"
ElseIf C = 3 Then
FillColor = "Red"
ElseIf C = 7 Then
FillColor = "Pink"
ElseIf C = 38 Then
FillColor = "Rose"
ElseIf C = 53 Then
FillColor = "Brown"
ElseIf C = 46 Then
FillColor = "Orange"
ElseIf C = 45 Then
FillColor = "Light Orange"
ElseIf C = 44 Then
FillColor = "Gold"
ElseIf C = 40 Then
FillColor = "Tan"
ElseIf C = 52 Then
FillColor = "Olive Green"
ElseIf C = 12 Then
FillColor = "Dark Yellow"
ElseIf C = 43 Then
FillColor = "Lime"
ElseIf C = 6 Then
FillColor = "Yellow"
ElseIf C = 36 Then
FillColor = "Light Yellow"
ElseIf C = 51 Then
FillColor = "Dark Green"
ElseIf C = 10 Then
FillColor = "Green"
ElseIf C = 50 Then
FillColor = "Sea Green"
ElseIf C = 4 Then
FillColor = "Bright Green"
ElseIf C = 35 Then
FillColor = "Light Green"
ElseIf C = 49 Then
FillColor = "Dark Teal"
ElseIf C = 14 Then
FillColor = "Teal"
ElseIf C = 42 Then
FillColor = "Aqua"
ElseIf C = 8 Then
FillColor = "Turquoise"
ElseIf C = 34 Then
FillColor = "Light Turquoise"
ElseIf C = 11 Then
FillColor = "Dark Blue"
ElseIf C = 5 Then
FillColor = "Blue"
ElseIf C = 41 Then
FillColor = "Light Blue"
ElseIf C = 33 Then
FillColor = "Sky Blue"
ElseIf C = 37 Then
FillColor = "Pale Blue"
ElseIf C = 55 Then
FillColor = "Indigo"
ElseIf C = 47 Then
FillColor = "Blue Gray"
ElseIf C = 13 Then
FillColor = "Violet"
ElseIf C = 54 Then
FillColor = "Plum"
ElseIf C = 39 Then
FillColor = "Lavender"
ElseIf C = 56 Then
FillColor = "Grey-80%"
ElseIf C = 16 Then
FillColor = "Grey-50%"
ElseIf C = 48 Then
FillColor = "Grey-40%"
ElseIf C = 15 Then
FillColor = "Grey-25%"
ElseIf C = 2 Then
FillColor = "White"
Else
FillColor = "NonStnd"
End If
Application.Calculate
End Function