使用宏替换字符串

时间:2012-09-13 16:11:34

标签: excel vba excel-vba

我想替换excel文件中所有工作表的字符串。

这是模块,我已经写过了。但是,我想从搜索(和替换)操作中排除A列。

我不知道该怎么做。你能否告诉我模块中的变化,我可以从searh中排除列。

由于

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 个答案:

答案 0 :(得分:3)

这可以解决你的麻烦:

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