繁体   English   中英

根据其他单元格的字体颜色改变单元格的值

[英]Change the value of the cell according to the font colour of the other cell

我想知道如何根据另一个单元格的字体颜色更改单元格的值。 在三个相邻的列(a,b,c)中,我有三个不同的值,在其中一个单元格上,我有一个绿色字体的值。

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

在此处输入图像描述

我尝试创建一个函数来查找字体的颜色,然后尝试在 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方法在这里说明: 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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM