Results 1 to 17 of 17

Thread: Multi Find and Replace VBA Script Macro Not Working

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,882
    Location
    Well, since you asked

    I'd turn it around a little

    In the WB#1 with the lists, I'd have the macro below. It uses a list in the WB#1 with the From-To pairs

    It opens a second WB#2 and replaces From-To pairs in WB#1 on all sheets in that WB#2

    If then saves and closes WB#2

    Seems to me musch easier than having the macro in many WBs and the From-To pairs in a 'database' WB



    Option Explicit
    
    'ORIGINAL SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
    'Adapted for external data
    
    Sub Multi_FindReplace()
     'PURPOSE: Find & Replace a list of text/values throughout entire workbook
        Dim wbReplaceList As Workbook
        Dim wbReplaceIn As Workbook
        Dim ws As Worksheet
        Dim sReplaceIn As String
        Dim rReplaceList As Range
        Dim iReplace As Long
        
        
        'get WB name to replace in
        sReplaceIn = Application.GetOpenFilename("File to Replace In, *.xls?", 1, "Select File To Replace In")
        If sReplaceIn = "False" Then Exit Sub
        
        Application.ScreenUpdating = False
        
        Set wbReplaceIn = Workbooks.Open(sReplaceIn)
        Set wbReplaceList = ThisWorkbook
        Set rReplaceList = wbReplaceList.Worksheets(1).Cells(1, 1).CurrentRegion
        For Each ws In wbReplaceIn.Worksheets
            With rReplaceList
                For iReplace = 2 To .Rows.Count
                    Call ws.UsedRange.Replace(.Cells(iReplace, 1).Value, .Cells(iReplace, 2).Value, xlWhole, , False)
                Next iReplace
            End With
        Next
        
        wbReplaceIn.Save
        wbReplaceIn.Close
        
        
        Application.ScreenUpdating = False
    End Sub

    MultiFind.xlsm has the macro and the From-To list, and ReplaceInHere.xlsx was my test WB to process
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •