Multiple Find and Replace in Multiple Files - Faster to use Array or Dictionary over Replace?

rdetreville

New Member
Joined
Jul 23, 2014
Messages
38
Hello,

I have a few hundred files with incorrect staff names on several worksheets, all located in sub-folders. I put together a macro that will open each Excel file, look down a list of incorrect names in the "fixnames.xlsm" workbook that houses the macro (Column 1) and replaces them with the corrected name in Column 2.

It seems to be working (though it sometimes doesn't seem to look at all worksheets) but is EXTREMELY SLOW. It will take hours to run this macro. I've already set to manual calculation and have disabled screen updating.

Is there some sort of array or dictionary function I could use to make this more efficient? Would it be faster to hard code the corrections into the macro instead of have them be retrieved from a spreadsheet? Right now it's Replacing by Cell - which is likely why it is so slow. Is there a way to have it perform this task faster? I'm desperate - have been searching for two days and no one has helped me. I have a meeting in a few hours and I'm expected to have this finished.

Thanks,

Richard


Code:
Sub test()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objSubFolder As Object
    Dim objFile As Object
    Dim MyFolder As String
    Dim wkbOpen As Workbook
    Dim wkb As Workbook
    Dim wks As Worksheet
    Dim CalcMode As Long
    
       
             Dim rList As Range, cell As Range

 Dim wkboook As Workbook
 Dim sht As Worksheet
 

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
            
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select tracker folder..."
        If .Show Then
            MyFolder = .SelectedItems(1)
            If Not Right(MyFolder, 1) = "\" Then
                MyFolder = MyFolder & "\"
            End If
        Else
            'No folder selected
        End If
    End With
    
                  
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(MyFolder)
    Set wkb = ActiveWorkbook
    Set wks = ActiveSheet
     Set wkboook = Workbooks("fixnames.xlsm")
    With wkboook.Sheets(1)
        Set rList = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    End With
    
    For Each objSubFolder In objFolder.SubFolders
        For Each objFile In objSubFolder.Files
            Set wkbOpen = Workbooks.Open(objFile.Path)
        For Each sht In ActiveWorkbook.Worksheets
        For Each cell In rList
        ActiveSheet.Cells.Replace What:=cell.Value, _
                                  Replacement:=cell.Offset(0, 1).Value, _
                                  LookAt:=xlWhole, _
                                  MatchCase:=False
                                  
    Next cell
    Next sht
           
            wkbOpen.Close savechanges:=True
        Next objFile
    Next objSubFolder
    
    With Application
        .Calculation = CalcMode
        .ScreenUpdating = True
        .EnableEvents = True
    End With

    MsgBox "Completed...", vbInformation
    
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,203,625
Messages
6,056,381
Members
444,862
Latest member
more_resource23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top