【问题标题】:search entire Excel workbook for text string and highlight cell在整个 Excel 工作簿中搜索文本字符串并突出显示单元格
【发布时间】:2017-08-07 12:43:09
【问题描述】:

我需要搜索包含多张工作表的整个 Excel workbook 可能出现在多列中的文本字符串 (比如说在 A 列到 J 列的范围内)

找到文本字符串后,它会将颜色格式应用于单元格。

这可能吗,还是我必须为每张纸制定规则?

一个例子:

  • 在我的workbook 中的任意位置查找字符串“信息”并将单元格设置为蓝色

我要输入多个不同的文本字符串,每个字符串都有不同的颜色格式。
有没有办法将它们全部组合在一个规则中,还是只需要让我为每个规则创建一个新规则,使用为每个文本字符串修改的相同规则?

我对 Excel 中的条件格式非常陌生,所以如果你能温柔地指导我完成每一步,我将不胜感激。

我已经搜索了 oracle 互联网并找到了这个解决方案。我需要对其进行测试,但它可能会满足我的需求。

这需要我构建一个名为 ChooseColors 的表。第一列是搜索字符串,第二列是颜色——从可用范围中挑选。 搜索区域在第二张纸上——从这张纸开始。

代码:

Sub DoColors()
Dim Picker As Variant
Dim Colors As Variant
Dim Rws As Long, j As Long
Dim i As Integer
Dim Sht As String
Dim c As Range
Dim FirstAddress

Sht = ActiveSheet.Name
'load search strings and colors into arrays
Application.Goto Reference:="ChooseColors"
ReDim Picker(1 To Selection.Rows.Count)
ReDim Colors(1 To Selection.Rows.Count)
For i = 1 To Selection.Rows.Count
Picker(i) = ActiveCell.Value
Colors(i) = ActiveCell.Offset(0, 1).Interior.ColorIndex
ActiveCell.Offset(1, 0).Select
Next i
'search the test range, changing backgrounds as required
Sheets(Sht).Activate
For i = 1 To UBound(Picker)
With Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
    Set c = .Find(Picker(i), LookIn:=xlValues)
    If Not c Is Nothing Then
        FirstAddress = c.Address
        Do
            c.Interior.ColorIndex = Colors(i)
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAddress
    End If
End With
Next i

End Sub

【问题讨论】:

  • 新条件和新颜色必须是新规则
  • 您也使用 [excel-vba] 标记您的问题,因此您可以编写 vba 解决方案?
  • 你肯定需要一个宏。 VBA 来拯救这里! :D
  • 我不介意它的 VBA,因为工作表中已经有一些其他 VBA 解决方案并且已经启用了宏
  • 编辑您的 OP 并在那里发布您的代码 - 很难在评论中阅读它...

标签: vba excel


【解决方案1】:

当需要时,我完全支持一个好的 VBA 解决方案……但在这里你最好使用 Find + Replace (Ctrl + H / Cmd + H)。单击“选项”按钮,在替换旁边,您可以格式化文本以替换它。选择“格式”,然后填充蓝色。将“信息”放在查找和替换字段中并运行:)

【讨论】:

  • 谢谢,是的,这适用于一个字符串,但我有多个字符串和多种不同的颜色要应用。话虽如此,我花了一些时间来找到我可以适应的 VBA 代码,我可能已经完成了查找和替换。
【解决方案2】:

此代码基于您发布的第一组代码,将突出显示您在工作簿中输入的所有文本。

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

    FindString = InputBox("Information")

    '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("A1"), _
            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.Interior
                    .ColorIndex = 6
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                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

要查找多个值和格式,您可以使用以下代码。
它依赖于我称为 Info 的工作表,其中包含要在 A1:A3 范围内查找的值。
这些值的背景已根据需要着色,代码只是找到匹配的值并复制颜色。

您可以添加额外的代码以允许更多值,或使用动态命名范围来返回您的源值。
动态命名范围将包含一个公式,例如:=Info!$A$1:INDEX(Info!$A:$A,COUNTA(Info!$A:$A)),给定名称为“SourceValues”。
Formula 功能区上选择Define Name,然后将公式粘贴到Refers To: 框中,并将SourceValues 粘贴到名称中。

然后,您将使用 Set Information = Range("SourceValues") 引用范围

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 InfoBit As Range
    Dim Information As Range

    Set Information = Range("SourceValues")
    'Set Information = ThisWorkbook.Worksheets("Info").Range("A1:A3")

    'Use For...Each to cycle through the information we're looking for.
    For Each InfoBit In Information
        'Use For...Each to cycle through the Worksheets collection.
        For Each wrkSht In ThisWorkbook.Worksheets
            'Ignore the "Info" sheet as it holds our values to search for.
            If wrkSht.Name <> "Info" Then
                'Find the first instance on the sheet.
                Set FoundCell = wrkSht.Cells.Find( _
                    What:=InfoBit, _
                    After:=wrkSht.Range("A1"), _
                    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
                        'Copy all formatting - bit of screen flicker.
'                        InfoBit.Copy
'                        FoundCell.PasteSpecial Paste:=xlPasteFormats

                        'Just copy the Interior colour.
                        FoundCell.Interior.Color = InfoBit.Interior.Color

                        'Look for the next instance on the same sheet.
                        Set FoundCell = wrkSht.Cells.FindNext(FoundCell)
                    Loop While FoundCell.Address <> FirstAddress
                End If
            End If
        Next wrkSht
    Next InfoBit

End Sub

【讨论】:

  • 谢谢。我给出的第一个示例只找到了一个特定的字符串,但我需要代码来搜索多个字符串并应用多种不同的颜色。我在上面发布的那个有“一半”的工作。我在这一行出现错误,不知道为什么 - Loop While Not c Is Nothing And c.Address FirstAddress
  • 我添加了代码来查找多个字符串。只需要添加一个包含这些多个字符串的新工作表。
  • 谢谢达伦,你让我有点不知如何修改它以包含多个字符串。
  • 添加命名范围:- 在Formulas 功能区上选择Name Manager。单击New 并将我提供的公式添加到Refers to: 框中,记住在公式中更新工作表名称(它们必须在其中,否则公式将引用活动表)。给它一个名字(SourceValues),然后将这一行:Set Information = ThisWorkbook.Worksheets("Info").Range("A1:A3") 更改为Set Information = Range("SourceValues"),代码应该很好。
  • 好吧,我已经试过了,但我完全迷失了,我把它归结为工作日结束大脑崩溃。我将在星期三回来,并试图让它发挥作用。我不知道你想让我把这个公式放在哪里,以及为什么它不起作用。另外,我在您告诉我要替换的行上出现错误。
猜你喜欢
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
相关资源
最近更新 更多