按List的FindReplace宏仅替换List中的第一个元素

时间:2014-04-25 21:41:09

标签: excel-vba vba excel

我的FindReplace宏只替换了数组中的第一个元素

我无法弄清楚出了什么问题

由于

Sub sReplace()
 'Column header for Search
 'List of words to look for
 'Word to Change elements is list to

fReplace "location", Array("Saint Paul", "SAINT PAUL", "St Paul", "St. Paul, St. Paul"), "Minneapolis-St. Paul"

End Sub

Sub fReplace(colHeader As String, LookFor As Variant, ReplaceWith As String)
Dim ws As Worksheet
Dim aCell

Set ws = ActiveWorkbook.Sheets("Helper")

For Each aCell In LookFor
Set aCell = ws.Rows("1:1").Find(What:=colHeader, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

If Not aCell Is Nothing Then
    With ws.Range(aCell.Offset(1, 0), ws.Cells(ws.Rows.count, aCell.Column).End(xlUp))
         .Replace What:=LookFor, Replacement:=ReplaceWith, _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
    End With
End If
Next aCell
End Sub

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

Sub sReplace()
    'Column header for Search
    'List of words to look for
    'Word to Change elements is list to

    fReplace "location", Array("Saint Paul", "SAINT PAUL", "St Paul", "St. Paul, St. Paul"), "Minneapolis-St. Paul"
End Sub

在上面的数组中,您可以移除"Saint Paul", "SAINT PAUL"中的任何内容,因为您使用的是MatchCase:=False

并更新了fReplace sub:

Sub fReplace(colHeader As String, LookFor As Variant, ReplaceWith As String)
    Dim ws As Worksheet
    Dim aCell As Range
    Dim lookForVal

    Set ws = ActiveWorkbook.Sheets("Helper")

    Set aCell = ws.Rows("1:1").Find(What:=colHeader, LookIn:=xlValues, _
                                    LookAt:=xlWhole, SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, MatchCase:=False, _
                                    SearchFormat:=False)

    If Not aCell Is Nothing Then
        For Each lookForVal In LookFor
            aCell.EntireColumn.Replace What:=lookForVal, Replacement:=ReplaceWith, _
                                        LookAt:=xlPart, SearchOrder:=xlByRows, _
                                        MatchCase:=False, SearchFormat:=False, _
                                        ReplaceFormat:=False
        Next lookForVal
    End If
End Sub

但请注意,因为您在LookAt:=xlPartReplace方法中使用"Minneapolis-St. Paul""St. Paul"部分"Minneapolis-St. Paul"可以替换为"Minneapolis-Minneapolis-St. Paul"获取"St. Paul"(因为LookFor位于{{1}}数组中)。

答案 1 :(得分:1)

替代(提供同样的提示simoco)

Sub fReplace(sColHeader As String, varFind As Variant, sReplace As String)

    Dim ws As Worksheet
    Dim rngFound As Range
    Dim sFind As Variant
    Dim sFirst As String

    Set ws = ActiveWorkbook.Sheets("Helper")
    Set rngFound = ws.Rows(1).Find(sColHeader, ws.Cells(1, ws.Columns.Count), xlValues, xlWhole)
    If Not rngFound Is Nothing Then
        sFirst = rngFound.Address
        Do
            With Range(rngFound.Offset(1), ws.Cells(ws.Rows.Count, rngFound.Column).End(xlUp))
                For Each sFind In varFind
                    .Replace sFind, sReplace, xlPart
                Next sFind
            End With
            Set rngFound = ws.Rows(1).Find("location", rngFound, xlValues, xlWhole)
        Loop While rngFound.Address <> sFirst
    End If

End Sub