I am a VBA beginner I am trying to build a simple comparison function which will compare two adjacent cells in Excel and return desired text values as output. Moreover I want to format the second cell according to the output.
The first part works well but the formatting part is giving me a hard time.
Function COMPARE(ByVal Cell1 As Range, ByVal Cell2 As Range) As String
Dim Result As String
If Cell1 = Cell2 Then
COMPARE = ""
ElseIf Cell1 > Cell2 Then
COMPARE = "Kapa-Reduzierung"
Cell2.Interior.Color = RGB(255, 153, 153)
ElseIf Cell1 < Cell2 Then
COMPARE = "Kapa-Erhöhung"
Cell2.Interior.Color = RGB(255, 255, 204)
Else: COMPARE = "Input Error"
End If
End Function
How to correctly write the following line of code? Cell2.Interior.Color = RGB........
1条答案
按热度按时间gzszwxb41#
这样如何?(更改工作簿名称、工作表名称和单元格范围以满足您的要求。)