简体   繁体   English

vba excel以黄色突出显示单元格

[英]vba excel to highlight cell in yellow

How can highlight in yellow a cell that have a specific word in it?如何以黄色突出显示包含特定单词的单元格?
I have data in colum B and F with the word "No Game".我在 B 栏和 F 栏有数据,上面写着“No Game”。
How can I have this in a vba in excel?我怎样才能在excel的vba中使用它?

Thanks谢谢

Although this question has already been answered, I'd take my chance, showing how easy this is using conditional formatting (screenshots are minimised a bit):尽管已经回答了这个问题,但我还是要抓住机会,展示一下使用条件格式是多么容易(屏幕截图被最小化了一点):

在此处输入图片说明

在此处输入图片说明

Result looks like this:结果如下所示:

在此处输入图片说明

Good luck祝你好运

Highlight Matches ( For Each...Next )突出显示匹配( For Each...Next

  • Copy the complete code into a standard module, eg Module1 .将完整代码复制到标准模块中,例如Module1
  • Adjust (play with) the values in the constants section.调整(使用)常量部分中的值。
Option Explicit

Sub HighlightColumns()
' Needs the 'RefColumn' and 'RefCombinedRange' functions.
    Const ProcTitle As String = "Highlight Columns"

    Const wsName As String = "Sheet1"
    Const FirstCellsList As String = "B2,H2"
    Const hCriteria As String = "No Game"
    Const hColor As Long = vbYellow
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
    
    ' Write the list of the first cells' addresses to an array ('FirstCells').
    Dim FirstCells() As String: FirstCells = Split(FirstCellsList, ",")
    
    Dim scrg As Range ' Source Column Range
    Dim sfCell As Range ' Source First Cell
    Dim sCell As Range ' Source Cell
    Dim hrg As Range ' Highlight Range
    Dim n As Long ' Columns Counter
    
    ' Combine all matching cells into the Highlight Range.
    For n = 0 To UBound(FirstCells)
        Set sfCell = ws.Range(FirstCells(n))
        Set scrg = RefColumn(sfCell)
        If Not scrg Is Nothing Then ' found data in column range
            For Each sCell In scrg.Cells
                If StrComp(CStr(sCell.Value), hCriteria, vbTextCompare) = 0 Then
                    Set hrg = RefCombinedRange(hrg, sCell)
                'Else ' not a match
                End If
            Next sCell
            Set scrg = Nothing
        'Else ' no data in current column range
        End If
    Next n
    
    ' Highlight and inform.
    If Not hrg Is Nothing Then ' Highlight Criteria found
        hrg.Interior.Color = hColor
        MsgBox "Highlighted cells equal to '" & hCriteria & "'.", _
            vbInformation, ProcTitle
    Else ' no Highlight Criteria found
        MsgBox "No occurrences of '" & hCriteria & "' found.", _
            vbExclamation, ProcTitle
    End If
    
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Creates a reference to the one-column range from the first cell
'               of a range ('FirstCell') to the bottom-most non-empty cell
'               of the first cell's worksheet column.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefColumn( _
    ByVal FirstCell As Range) _
As Range
    If FirstCell Is Nothing Then Exit Function
    
    With FirstCell.Cells(1)
        Dim lCell As Range
        Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , , xlPrevious)
        If lCell Is Nothing Then Exit Function
        Set RefColumn = .Resize(lCell.Row - .Row + 1)
    End With

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Creates a reference to a range combined from two ranges.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefCombinedRange( _
    ByVal CombinedRange As Range, _
    ByVal AddRange As Range) _
As Range
    If CombinedRange Is Nothing Then
        Set RefCombinedRange = AddRange
    Else
        Set RefCombinedRange = Union(CombinedRange, AddRange)
    End If
End Function

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

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