search entire Excel workbook for text string and h

2019-09-25 12:49发布

I need to search an entire Excel workbook containing multiple sheets for text strings that may be present in multiple columns (say in the range column A to column J)

When a text string is found, it applies a color format to the cell.

Is this possible, or will I have to make a rule for each sheet?

An example:

  • Find string "information" anywhere in my workbook and format cell blue

I have multiple different text strings to enter and each will have a different color format.
Is there a way to combine them all in one rule, or will this just need to have me create a new rule for each, using the same rule modified for each text string?

I'm really new at the conditional formatting in Excel, so if you could be gentle and walk me through each step that would be appreciated.

I have searched the oracle internet and found this solution. I need to test it but it may do what I need.

This requires me to build a table called ChooseColors. The first column is the search string, the second is the colors -- pick from the available range. The search area is on a second sheet -- start on this sheet.

Code:

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

2条回答
别忘想泡老子
2楼-- · 2019-09-25 13:32

This code, based on the first set of code you posted, will highlight all occurrences of whatever text you type in within the workbook.

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

To find more than one value and format you could use the following code.
It relies on a sheet I've called Info which has the values to look for in the range A1:A3.
The background to these values have been coloured as you want and the code just finds the matching values and copies the colour over.
enter image description here

You could add extra code to allow for more values, or use a dynamic named range to return your source values.
A dynamic named range would consist of a formula such as: =Info!$A$1:INDEX(Info!$A:$A,COUNTA(Info!$A:$A)) with a given name of 'SourceValues'.
Select Define Name on the Formula ribbon and paste the formula into the Refers To: box and the SourceValues into the name.
enter image description here
You'd then refer to the range with 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

enter image description here

查看更多
贪生不怕死
3楼-- · 2019-09-25 13:37

I'm all for a good VBA solution when it is needed... but here you are better off using Find + Replace (Ctrl + H / Cmd + H). Click the "Options" button and next to the replace you can format the text to replace it with. Select "Format" then fill then the blue color. Put "information" in both the find and replace fields and run :)

查看更多
登录 后发表回答