Enrico Giai
Enrico Giai

Reputation: 7

Replacing text in same column for all sheets

I have a large Excel file with several sheets. They all share the same structure. I need to replace "old1" and "old2" with "new1" and "new2" in column E of all sheets. This is what I came up with:

`Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long

fndList = Array("old1", "old2")
rplcList = Array("new1", "new2")

'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
    'Loop through each worksheet in ActiveWorkbook
    For Each sht In ActiveWorkbook.Worksheets
        sht.Range("E:E").Replace What:=fndList(x), Replacement:=rplcList(x), _
        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
        SearchFormat:=False, ReplaceFormat:=False
    Next sht
Next x`

Unfortunately though this replaces the text in ALL cells, not just in E:E. What should I do? Thanks in advance! Enrico

Upvotes: 0

Views: 140

Answers (1)

Gary's Student
Gary's Student

Reputation: 96763

Perhaps:

Sub GettingOlder()
    Dim s As Worksheet
    For Each s In Sheets
        With s
            .Range("E:E").Replace What:="old1", Replacement:="new1"
            .Range("E:E").Replace What:="old2", Replacement:="new2"
        End With
    Next s
End Sub

Before:

enter image description here

After:

enter image description here

Upvotes: 2

Related Questions