EXCEL中如何根据A1单元格字体颜色,B1显示特定内容?

如:
A1单元格内字体为红色,则F1单元格显示"某某值超过范围"
A1、B1单元格内字体为红色,则F1单元格显示"某某值、某某值超过范围"
A1、B1、C1单元格内字体为红色,则F1单元格显示"某某值、某某值、某某值超过范围"

打开你的Excel文件,按“Alt+F11”打开VBA编辑窗口,然后在左侧对应的Sheet上双击,右侧空白处粘贴下面的代码。关闭VBA窗口。然后按“Alt+F8”打开宏窗口,选择刚插入的宏,点击“执行”。



Sub bs()
Dim i, j As Long
Dim s As String
Columns("F").ClearContents
For i = 1 To UsedRange.SpecialCells(xlCellTypeLastCell).Row
    For j = 1 To 5
        If Cells(i, j).Font.Color = RGB(255, 0, 0) Then
            If s = "" Then s = Cells(i, j).Value Else s = s & "、" & Cells(i, j).Value
        End If
    Next
    If s <> "" Then
        Cells(i, "F").Value = s & "超过范围": s = ""
    End If
Next
End Sub

追问

不好意思,有劳大神解释下我该修改哪些部分,能达到目的?
T列到AA列的数据中,任一单元格内容字体为红色,则AB列单元格显示汉字”某值超过范围“,如有两个单元格内容字体为红色,则AB列单元格显示汉字”某值、某值超过范围“,如有三个单元格内容字体为红色,则AB列单元格显示汉字”某值、某值、某值超过范围“

追答

好的,代码更改如下:

Sub bs()
Dim i, j As Long
Dim s As String
Columns("AB").ClearContents
For i = 1 To UsedRange.SpecialCells(xlCellTypeLastCell).Row
    For j = Range("T1").Column To Range("AA1").Column
        If Cells(i, j).Font.Color = RGB(255, 0, 0) Then
            If s = "" Then s = Cells(i, j).Value Else s = s & "、" & Cells(i, j).Value
        End If
    Next
    If s <> "" Then
        Cells(i, "AB").Value = s & "超过范围": s = ""
    End If
Next
End Sub

追问

真心不好意思,大神,单元格内显示为汉字内容,
如:"甲超过范围",
”甲、乙超过范围“,
”甲、乙、丙超过范围“

追答

这个不分数字和汉字,只要红色就列出来。如下图测试结果。


追问

我的意思是,AB里显示的是我自己写的汉字内容,不要之前的单元格内的数值或内容。麻烦大神了。
如:T1=10,U1=20,V1=30,W1=40,且其中10、20、30均为红色字体,
则AB=甲、乙、丙超过范围
不是要AB=10、20、30超过范围。。。

追答

额……是这个意思么?第一行是标题行,显示标题的内容超过范围?

Sub bs()
Dim i, j As Long
Dim s As String
Columns("AB").ClearContents
For i = 1 To UsedRange.SpecialCells(xlCellTypeLastCell).Row
    For j = Range("T1").Column To Range("AA1").Column
        If Cells(i, j).Font.Color = RGB(255, 0, 0) Then
            If s = "" Then s = Cells(1, j).Value Else s = s & "、" & Cells(1, j).Value
        End If
    Next
    If s <> "" Then
        Cells(i, "AB").Value = s & "超过范围": s = ""
    End If
Next
End Sub

温馨提示:答案为网友推荐,仅供参考
相似回答