Excel User Defined Function: change the cell's color

前端 未结 6 1048
生来不讨喜
生来不讨喜 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:51

    You could create a vba code that runs automatically after there is a change in your sheet. Instead of hving the code in a seperate module you have to embed it in the sheet itself.

    Right click on the sheet tab, choose View Code, and create the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    For Each cell In Range("A1:B8") 'change cell range as needed
    
    Select Case cell.Value
    Case 8
    cell.Interior.ColorIndex = 4 'cell color becomes green when cell value is 8
    Case ""
    cell.Interior.ColorIndex = 1 'cell color becomes black when cell is empty
    Case Is < 6
    cell.Interior.ColorIndex = 7 'cell color becomes pink when cell value is smaller than 6
    Case Else
    cell.Interior.ColorIndex = 0 'all other cells get no color
    End Select
    
    Next cell
    
    End Sub
    

提交回复
热议问题