【问题标题】:Find string, change color across all Excel Worksheets查找字符串,更改所有 Excel 工作表的颜色
【发布时间】:2020-05-09 07:30:45
【问题描述】:

search entire Excel workbook for text string and highlight cell 似乎正是我所需要的,但我无法让它在我的 Excel 工作簿上工作。我在 10 个工作表中有数百行。所有搜索到的字符串(数据包 01、数据包 02、数据包 03 等)将位于 B:8 到 worksheet(1) 的行端和 B:7 到其他 9 的行端工作表(工作表被命名并且字符串的InputBox 结果需要区分大小写)。 45547221 表示内部颜色变化,但是所有字符串都有不同颜色的单元格时颜色会太多,因此使用font.color.index 更改字符串颜色会更好。按原样尝试 45547221 代码会发现它在步进模式下跳过了 Do/Loop While 代码。

我会修改 45547221 中的代码,至少添加:

Dim myColor As Integer
myColor = InputBox("Enter Color Number (1-56)")

(配置后我可以使用 InputBox(es) 输入多达 5 个 FindStrings 和 5 个 ColorIndex 数字作为 Dim) 在Do/Loop While 我会更改.ColorIndex = myColor

我想让这段代码工作,因为它似乎符合我的需要 - 修改为在工作簿中查找字符串实例并重新着色字符串而不是单元格内部颜色,以及 (2) 让它识别 Do/Loop While 代码现在不是,但会将ColorIndex 数字应用于每个字符串。


Public Sub find_highlight()

    'Put Option Explicit at the top of the module and
    'Declare your variables.
    Dim FindString As String
    Dim wrkSht As Worksheet
    Dim FoundCell As Range
    Dim FirstAddress As String
    Dim MyColor As Integer 'Added this

    FindString = InputBox("Enter Search Word or Phrase")
    MyColor = InputBox("Enter Color Number")

    'Use For...Each to cycle through the Worksheets collection.
    For Each wrkSht In ThisWorkbook.Worksheets
        'Find the first instance on the sheet.
        Set FoundCell = wrkSht.Cells.Find( _
            What:=FindString, _
            After:=wrkSht.Range("B1"), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False)
        'Check it found something.
        If Not FoundCell Is Nothing Then
            'Save the first address as FIND loops around to the start
            'when it can't find any more.
            FirstAddress = FoundCell.Address
            Do
                With FoundCell.Font 'Changed this from Interior to Font
                    .ColorIndex = MyColor
                    '.Pattern = xlSolid
                    '.PatternColorIndex = xlAutomatic 'Deactivated this
                End With
                'Look for the next instance on the same sheet.
                Set FoundCell = wrkSht.Cells.FindNext(FoundCell)
            Loop While FoundCell.Address <> FirstAddress
        End If

    Next wrkSht

End Sub

【问题讨论】:

  • 在您的问题中包含代码当您尝试修改它时,并准确解释它是如何不起作用的(或者如果这是问题,您会遇到什么错误)。由于链接的代码似乎可以正常工作,因此无法准确猜出它为什么不适合您。
  • First... 尝试使用 在这里设置代码但失败了。我用 5 个工作表构建了一个独立文件。修改为将 Dim MyColor 添加为 Integer,并将 MyColor = InputBox("Color") 和 .ColorIndex = [Number] 更改为 .ColorIndex = MyColor,然后将 With FoundCell.Interior 更改为 With FoundCell.Font 并删除 .Pattern = xlSolid 和 .PatternColorIndex = 自动。原始代码和修改后的代码在单机上工作。我把它放在工作簿中,我需要它作为 ThisWorkbook 模块,并在逐步突出显示 If Not FoundCell Is Nothing 时,它跳过了 Do 代码。
  • 如果您将完整代码添加到问题中,有人会为您设置格式。
  • 蒂姆,谢谢。认为 Stackhouse 参考就足够了。用代码编辑了帖子。

标签: excel vba string colors


【解决方案1】:

编辑:这对您的示例数据有用,使用部分匹配,因此您可以输入(例如)“数据包 03”并且仍然匹配。

我喜欢将“查找所有”功能拆分为一个单独的功能:它使其余的逻辑更容易理解。

Public Sub FindAndHighlight()

    Dim FindString As String
    Dim wrkSht As Worksheet
    Dim FoundCells As Range, FoundCell As Range
    Dim MyColor As Integer 'Added this
    Dim rngSearch As Range, i As Long, rw As Long

    FindString = InputBox("Enter Search Word or Phrase")
    MyColor = InputBox("Enter Color Number")

    'Cycle through the Worksheets
    For i = 1 To ThisWorkbook.Worksheets.Count

        Set wrkSht = ThisWorkbook.Worksheets(i)

        rw = IIf(i = 1, 8, 7) '<<< Row to search on
                              '    row 8 for sheet 1, then 7

        'set the range to search
        Set rngSearch = wrkSht.Range(wrkSht.Cells(rw, "B"), _
                        wrkSht.Cells(Rows.Count, "B").End(xlUp))

        Set FoundCells = FindAll(rngSearch, FindString) '<< find all matches

        If Not FoundCells Is Nothing Then
            'got at least one match, cycle though and color
            For Each FoundCell In FoundCells.Cells
                FoundCell.Font.ColorIndex = CInt(MyColor)
            Next FoundCell
        End If

    Next i

End Sub

'return a range containing all matching cells from rng
Public Function FindAll(rng As Range, val As String) As Range
    Dim rv As Range, f As Range
    Dim addr As String

    'partial match...
    Set f = rng.Find(what:=val, after:=rng.Cells(rng.Cells.CountLarge), _
        LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=True) 'case-sensitive
    If Not f Is Nothing Then addr = f.Address()

    Do Until f Is Nothing
        If rv Is Nothing Then
            Set rv = f
        Else
            Set rv = Application.Union(rv, f)
        End If
        Set f = rng.FindNext(after:=f)
        If f.Address() = addr Then Exit Do
    Loop

    Set FindAll = rv
End Function

【讨论】:

  • 在单步执行时,代码会前进并突出显示“Do until f Is Nothing”,然后不进行任何处理就跳到“Set FindAll = rv”行。我在一个干净的 Excel 上尝试了这个,我在每个工作表的 5 个位置都有 Packet 02(只有单元格中的字符串)。没有其他模块。我还尝试了我的大电子表格,它在字符串中包含搜索到的字符串,即 C:\Users\Richard\Desktop\STEPHANIE\Audios\Packet 02 - WAV\WAV File 02.wav。还有其他模块 - 您的代码在其工作表模块中。我很困惑。检查整个单元格和中间字符串。
  • Sheet1 列出了所有文件。我已经编码以查找文件扩展名并将行复制到适当的工作表,例如文档、音频等。该功能工作正常。另一种方法可能是在 Sheet1 上查找字符串并突出显示为 SUB,然后运行 ​​SUB 标识扩展名并复制行。这会有帮助吗?
  • 我听不懂你在说什么。请记住,我对您在做什么或您的工作簿是什么样子一无所知。我所看到的只是您发布的代码。
  • 我已经构建了一个电子表格。目标是在所有工作表中找到所有 FindStrings 并应用 MyColor。我将您的代码作为工作表模块。我在多台计算机上构造函数的其他代码,因此感到沮丧。我看不到上传 .xlsm 或 .zip 的方法。这可能吗?我是否会遗漏一些我需要单击、调用或检查的内容?
  • 如果您需要分享文件,您可以上传到 Dropbox/Box/Google 等并在此处分享链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 2019-03-06
  • 2012-04-13
  • 2020-12-07
  • 2013-03-30
相关资源
最近更新 更多