I have a function like so:
Function cellcolor(r As Range) As Long
cellcolor = r.Interior.Color
End Function
I then reference it in Cell B1 like so:
=cellcolor(A1)
The color of A1 is red, and therefore the output is 255.
I want to set the color of B1 to match that of A1 but I'm unsure how. Is it possible?
I've tried:
Function cellcolor(r As Range) As Long
cellcolor.Interior.Color = r.Interior.Color
End Function
But it doesn't work, any ideas?
Thanks
EDIT:
As pointed out in the comments, a function cannot make cell format changes. I therefore used the following:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
For Each ws In ActiveWorkbook.Worksheets
For Each cell In ws.Range("B1")
If Len(cell.Formula) > 0 Then
cell.Interior.ColorIndex = Range(cell.Formula).Interior.ColorIndex
End If
Next cell
Next ws
End Sub
Basically on cell change it finds which cell B1 is looking at, gets the target cell's color and copies it. It does this across all sheets in the workbook.
I know it's a bit weird but serves it's purpose. Thanks everyone!