Excel User Defined Function: change the cell's color

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

    Here's a demonstration of how a VBA UDF can change the colouring of a sheets contents rather than using conditional formatting.

    As long as both sheets have rows and columns sorted in the same order then this will compare for differences in every cell between two seperate Excel sheets.

    You can add this into as many cells as you need to on a third sheet to detect differences between the same two cells on the two sheets with data on: =DifferenceTest(Sheet1!A1,Sheet2!A1)

    And the function to be stored in the VBA editor as follows:

    Function DifferenceTest(str1 As String, str2 As String) As String
    
        If str1 = str2 Then
                Application.Caller.Font.ColorIndex = 2
        Else
                Application.Caller.Font.ColorIndex = 3
                DifferenceTest = str1 & " vs " & str2
        End If
    
    End Function
    

提交回复
热议问题