【问题标题】:Find and highlight a specific word in a range of cells在一系列单元格中查找并突出显示特定单词
【发布时间】:2018-11-27 22:11:28
【问题描述】:

我想在一系列单元格中找到一个特定的单词,然后用红色突出显示它。为此,我创建了这段代码,但它只在一行上运行并突出显示了所有单元格文本:

Sub Find_highlight()
    Dim ws As Worksheet
    Dim match As Range
    Dim findMe As String

    Set ws = ThisWorkbook.Sheets("MYSHEET")
    findMe = "Background"

    Set match = ws.Range("G3:G1362").Find(findMe)
    match.Font.Color = RGB(255, 0, 0)
End Sub

【问题讨论】:

  • @pnuts:我很抱歉。不知道该链接存在。我也将其投票为dup。 :)

标签: excel vba


【解决方案1】:

假设你的 excel 文件看起来像 htis

要为特定单词着色,您必须使用单元格的.Characters 属性。你需要找到单词从哪里开始,然后给它上色。

试试这个

Option Explicit

Sub Sample()
    Dim sPos As Long, sLen As Long
    Dim aCell As Range
    Dim ws As Worksheet
    Dim rng As Range
    Dim findMe As String

    Set ws = ThisWorkbook.Sheets("MYSHEET")

    Set rng = ws.Range("G3:G1362")

    findMe = "Background"

    With rng
        Set aCell = .Find(What:=findMe, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

        If Not aCell Is Nothing Then
            sPos = InStr(1, aCell.Value, findMe)
            sLen = Len(findMe)

            aCell.Characters(Start:=sPos, Length:=sLen).Font.Color = RGB(255, 0, 0)
        End If
    End With
End Sub

输出

【讨论】:

  • 你试过超过 1 行吗?
  • 不,我没有。我已经向您展示了方法,我相信您可以解决这个问题?
  • 我不知道,范围是指定的但它只执行只有一行的动作!!!
  • 你需要找到所有搜索词的起始位置,然后使用.Characters方法将它们全部着色。
  • 老实说,我看不到解决方案,因为我是 vba 的初学者。你的意思是我应该使用for
【解决方案2】:

我做了一些更改,使其更加通用和准确

Option Explicit
Sub HIGHLIGHTER()
Dim sPos As Long, sLen As Long
Dim rng As Range
Dim findMe As String
Dim i As Integer

Set rng = Application.InputBox(Prompt:= _
    "Please Select a range", _
    Title:="HIGHLIGHTER", Type:=8)
findMe = Application.InputBox(Prompt:= _
    "FIND WHAT?(YOU CAN USE PATTERN USED IN LIKE OPERATOR ", _
    Title:="HIGHLIGHTER", Type:=2)
  For Each rng In rng
    With rng
     If rng.Value Like "*" & findMe & "*" Then
        If Not rng Is Nothing Then
                   For i = 1 To Len(rng.Value)
                   sPos = InStr(i, rng.Value, findMe)
                   sLen = Len(findMe)
                   If (sPos <> 0) Then
                    rng.Characters(Start:=sPos, Length:=sLen).Font.Color = RGB(255, 0, 0)
                    i = sPos + Len(findMe) - 1
                   End If
                   Next i
       End If
     End If
    End With
   Next rng
End Sub

【讨论】:

    【解决方案3】:

    添加了循环选项

    Option Explicit
    
    Sub Macro1()
        Dim sPos As Long, sLen As Long
        Dim aCell As Range
        Dim ws As Worksheet
        Dim rng As Range
        Dim findMe As String
    
        Set ws = ThisWorkbook.Sheets("Sheet2")
    
        Set rng = ws.Range("A3:A322")
    
        findMe = "find"
    
       For Each rng In Selection
        With rng
            Set aCell = .Find(What:=findMe, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
    
            If Not aCell Is Nothing Then
                sPos = InStr(1, aCell.Value, findMe)
                sLen = Len(findMe)
    
                aCell.Characters(Start:=sPos, Length:=sLen).Font.Color = RGB(0, 255, 0)
            End If
        End With
        Next rng
    End Sub
    

    【讨论】:

      【解决方案4】:

      我也进行了一些更改以允许同时搜索多个单词。我还去掉了提示并对搜索词进行了硬编码。剩下的唯一问题是使搜索不区分大小写...

      Sub HIGHLIGHTER()
      Dim sPos As Long, sLen As Long
      Dim rng As Range
      Dim findMe As String
      Dim i As Integer
      Dim t As Integer
      Dim SearchArray
      
      SearchArray = Array("WORD1", "WORD2")
      
      For t = 0 To UBound(SearchArray)
      
          Set rng = Range("N2:N10000")
          findMe = SearchArray(t)
      
          For Each rng In rng
              With rng
                  If rng.Value Like "*" & findMe & "*" Then
                      If Not rng Is Nothing Then
                          For i = 1 To Len(rng.Value)
                              sPos = InStr(i, rng.Value, findMe)
                              sLen = Len(findMe)
      
                              If (sPos <> 0) Then
                                  rng.Characters(Start:=sPos, Length:=sLen).Font.Color = RGB(255, 0, 0)
                                  rng.Characters(Start:=sPos, Length:=sLen).Font.Bold = True
                                  i = sPos + Len(findMe) - 1
                              End If
                          Next i
                      End If
                  End If
              End With
          Next rng
      
      Next t
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 2011-04-12
        • 1970-01-01
        • 1970-01-01
        • 2015-11-03
        • 1970-01-01
        • 2019-06-24
        相关资源
        最近更新 更多