Replace string using a Macro

2019-09-08 18:21发布

I would like to replace the string from all the worksheet in an excel file.

Here is the module, I have written to do it. However, I would like to exclude Column A from the search( and replace ) operation.

I am not sure how to do it. Can you please let me know the changes in the module where I can exclude the column from the searh.

Thanks

Sub FindSignificant()
    Dim SearchString As String
    Dim SearchRange As Range, cl As Range
    Dim FirstFound As String
    Dim sh As Worksheet

    ' Set Search value
    SearchString = "SIGNIFICANT"
    Application.FindFormat.Clear
    ' loop through all sheets
    For Each sh In ActiveWorkbook.Worksheets
        ' Find first instance on sheet
        Set cl = sh.Cells.Find(What:=SearchString, _
             After:=sh.Cells(1, 1), _
            LookIn:=xlFormulas, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False)
        If Not cl Is Nothing Then
            ' if found, remember location
            FirstFound = cl.Address
            ' format found cell
            Do
                cl.Font.Bold = True
                cl.Font.ColorIndex = 3
                ' cl.Interior.ColorIndex = 3
                ' find next instance
                Set cl = sh.Cells.FindNext(After:=cl)
                ' repeat until back where we started
            Loop Until FirstFound = cl.Address
        End If
    Next
End Sub

1条回答
欢心
2楼-- · 2019-09-08 19:08

This should solve your trouble:

Do
  If not cl.column = 1 Then
    cl.Font.Bold = True
    cl.Font.ColorIndex = 3
    ' cl.Interior.ColorIndex = 3            
  End If
    ' find next instance
    Set cl = sh.Cells.FindNext(After:=cl)
    ' repeat until back where we started
Loop Until FirstFound = cl.Address
查看更多
登录 后发表回答