VBA |找到所有“文本”并将其字体颜色更改为红色

问题描述 投票:0回答:1

我想找到所有带有“未找到”文本的单元格,并将其字体设为红色。 关于如何做到这一点有什么建议吗?

字符串未找到=“未找到”

我的程序会调用子程序Find_NotFound

循环部分我不太确定,因为它应该查找直到所有“未找到”都为红色。

我在“Loop While”部分遇到错误

Sub Find_NotFound(NotFound As String)

    '============================================
    Application.ScreenUpdating = False
    Dim Find_NotFound As Range
    Range("A1").Select
    
    Set Find_NotFound = Cells.Find(What:=NotFound, After:=ActiveCell, LookIn:=xlFormulas2, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)
    
    
    'Start from A1 Cell, start Finding
    '===================================================================
    'If found "Not Found" word
    If Not Find_NotFound Is Nothing Then
    
        Find_NotFound.Activate
                    
        Do
                With Selection.Font
                    .Color = vbRed
                End With
                
                Cells.FindNext(After:=ActiveCell).Activate
            
        Loop While Cells.Selection.Font.Color <> vbRed

    End If
    
    Application.ScreenUpdating = True End Sub

亲切的问候

excel vba loops fonts find
1个回答
0
投票

这是一种不同且更快的方法,可以达到相同的结果:

Sub ColourNotFoundRed()
    Cells.Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""Not Found"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16776961
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.