Excel User Defined Function: change the cell's color

前端 未结 6 1142
生来不讨喜
生来不讨喜 2020-12-11 11:10

I have a user defined function in Excel. It is called as a formula function from spreadsheet cells and works fine.

I\'d like the function to be able to change the ce

6条回答
  •  生来不讨喜
    2020-12-11 11:38

    I tried the Evaluate method, which worked but immediately crashed (2007). The help mentions caching the address, so that's my approach - store the cell and color in a collection, then change the color after the calculation.

    Dim colorCells As New Collection
    
    Function UDF...
        UDF = 
        color = 
        colorCells.Add (Application.Caller)
        colorCells.Add (color)
    End Function
    
    Sub SetColor()
        While colorCells.Count <> 0
            colorCells(1).Interior.Color = colorCells(2)
            colorCells.Remove (1)
            colorCells.Remove (1)
        Wend
    End Sub
    
    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        SetColor
    End Sub
    

提交回复
热议问题