Share via

Find and Replace multiple values over multiple excel files, using a script/VBA/Macro

Anonymous
2023-01-18T23:01:15+00:00

Hello,

I have a situation where I have 100 or so excel files, which I need to search and replace multiple values in each.

I found this: https://answers.microsoft.com/en-us/msoffice/forum/all/find-and-replace-values-in-all-excel-files-in-a/deb409ac-8467-4648-a44d-f1dd47b7d45d

Which is brilliant and does what I want in part, however I want to modify it to instead of asking for the value to search for, and the value to replace it with, to instead use a table in the excel file where the macro resides, and search and replace all the values in the table that is listed there, with all the replacment values that are listed too. This would save hours and hours of time, if I can simply list all the values and their replacements to search for, select all the files that I want this work done on, and the script opens each file, replaces all the values it finds with the replacement values, and saves the file, and moves on to the next one.

Is this possible to do?

Can anyone assist with this please?

I am using Office365, latest version etc.

Thank you in advanced!

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. HansV 462.6K Reputation points MVP Volunteer Moderator
    2023-01-18T23:29:12+00:00

    Here you go:

    Sub ReplaceInFolder()
        ' ************** Change the constants as needed ***************
        Const ListSheet = "List" ' sheet with the find and replace text
        Const FindCol = "A"      ' column with the find text
        Const ReplaceCol = "B"   ' column with the replacement text
        Const FirstRow = 2       ' first row with find/replacement text
        ' *************************************************************
    
        Dim wshList As Worksheet
        Dim r As Long
        Dim LastRow As Long
        Dim strPath As String
        Dim strFile As String
        Dim wbk As Workbook
        Dim wsh As Worksheet
        Dim strFind As String
        Dim strReplace As String
    
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then
                strPath = .SelectedItems(1)
            Else
                MsgBox "No folder selected!", vbExclamation
                Exit Sub
            End If
        End With
        If Right(strPath, 1) <> "\" Then
            strPath = strPath & "\"
        End If
    
        Application.ScreenUpdating = False
        ' Set reference to the sheet with the find and replace text
        Set wshList = ThisWorkbook.Worksheets(ListSheet)
        ' Determine the last used row in the column with the find text
        LastRow = wshList.Cells(wshList.Rows.Count, FindCol).End(xlUp).Row
    
        strFile = Dir(strPath & "*.xls*")
        ' Loop through the workbooks
        Do While strFile <> ""
            ' Open workbook
            Set wbk = Workbooks.Open(Filename:=strPath & strFile, AddToMRU:=False)
            ' Loop through the worksheets
            For Each wsh In wbk.Worksheets
                ' Loop through the rows with find and replace text
                For r = FirstRow To LastRow
                    strFind = wshList.Cells(r, FindCol).Value
                    strReplace = wshList.Cells(r, ReplaceCol).Value
                    ' Replace
                    wsh.Cells.Replace What:=strFind, Replacement:=strReplace, _
                        LookAt:=xlWhole, MatchCase:=False
                Next r
            Next wsh
            ' Close and save workbook
            wbk.Close SaveChanges:=True
            strFile = Dir
        Loop
        Application.ScreenUpdating = True
    End Sub
    
    5 people found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points MVP Volunteer Moderator
    2023-01-19T11:06:05+00:00

    I'm afraid I don't know how to do that. The Replace method returns a Boolean (True/False) value, but it returns True even if nothing was actually replaced. And the Saved property of the workbook is changed from True to False even if nothing was replaced.

    An alternative would be to search for each instance individually and keeping a count of them, but that would be very slow.

    0 comments No comments
  2. Anonymous
    2023-01-19T01:29:58+00:00

    Sorry 1 more thing

    wsh.Cells.Replace What:=strFind, Replacement:=strReplace, LookAt:=xlWhole, MatchCase:=False  
    

    Is it possible to tap into the Return of this line, to see if a replacement has happened or not? Maybe make it a global which gets reset when it moves on to the next file, so any replacements that actually happened in that file will set it, and then trigger the save of that file.

    I have tried a few things, but I cant figure out the syntax for an If statement, or to tap into the return of this 'Replace' function.

    Such as if a replacement didn't happen anywhere in that File, then to skip the Save?

    I am just getting it saving the sheet even if a change didn't actually happen.

    Maybe its a version thing that when the file is opened it thinks a change has happened even if a replacement didn't happen. Some of the files I am dealing with are a few years old.

    I had experimented with saving it as current XLSX files, but I couldnt work out how to actually get rid of the original XLS file too. But that is another step for later I think.

    For now I am just trying to make it only save the file if a replacement from the list actually happened.

    Thanks in Advance

    0 comments No comments
  3. Anonymous
    2023-01-19T01:09:29+00:00

    oh actually, I see its in the constants...

    Just changed this to 'Sheet1' and all is fine now - thanks.
    You have been an amazing help!

    0 comments No comments
  4. Anonymous
    2023-01-19T01:05:18+00:00

    Amazing, thank you!

    I however get a 'subscript out of range' error on this line

    Set wshList = ThisWorkbook.Worksheets(ListSheet)

    0 comments No comments