【问题标题】:Change the value of a cell according to the font colour of another cell根据另一个单元格的字体颜色改变一个单元格的值
【发布时间】:2022-09-26 02:48:34
【问题描述】:

我想根据另一个单元格的字体颜色更改单元格的值。

在三个相邻的列中,我有三个不同的值。在其中一个单元格上,我有一个绿色字体的值。

我想做一个条件,根据哪个单元格包含带有绿色字体的文本/值,我可以选择该文本所在的列:

我试图创建一个函数来查找字体的颜色,然后在 if 语句中使用这个函数。

Function IsColor(lColor As Long, cl As Range) As Boolean
If cl.Font.ColorIndex = lColor Then
    IsColor = True
Else
    IsColor = False
End If
End Function
  • 你可以使用Find 方法——它有一个格式参数。
  • 你能用一个例子来说明你的答案吗?
  • 如果 \"B\" 和 \"C\" 中都有绿色文字,你要显示 \"B\" 还是 \"B, C\"?
  • 这些颜色的原因是什么?如果它们是由条件格式生成的,最好将列基于条件格式源而不是颜色。
  • 请注意Find 不适用于条件格式颜色:我刚刚创建了一个 Excel 文件,其中包含两个单元格,一个包含单词“Black”,另一个包含单词“Green”。我使用条件格式将“绿色”的颜色更改为...绿色,当然 :-) 使用Find 时,使用字体格式字体颜色,什么也没找到。

标签: excel vba if-statement


【解决方案1】:

使用您的功能,以下可能会解决您的问题:

find 方法在这里说明:VBA format cell based on fill color and font color

Option Explicit

Function IsColor(lColor As Long, cl As Range) As Boolean
If cl.Font.Color = lColor Then
    IsColor = True
Else
    IsColor = False
End If
End Function

Function findGreen(myRange As Range) As String
    Dim myCell As Range
    Dim tmpStr As String
    tmpStr = "no green color found!"
    For Each myCell In myRange
        'Debug.Print myCell.Address; myCell.Font.Color
        If IsColor(vbGreen, myCell) Then
            'Address or column ?!
            'tmpStr = myCell.Address
            tmpStr = "green in column " & Chr(64 + myCell.Column)
            Exit For
        End If
    Next myCell
    findGreen = tmpStr
End Function

Sub test_findGreen()
    Call findGreen(Range("A2:C2"))
End Sub

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多