+ Reply to Thread
Results 1 to 6 of 6

Find and replace in several (not all) sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    12-18-2012
    Location
    Almada
    MS-Off Ver
    Excel 2011
    Posts
    2

    Find and replace in several (not all) sheets

    Hello there,

    I need to find a quick/automatic way to execute a ca. 500 "find and replace" 's in more than 250 sheets.

    I prepared an excel file for a better understanding of the problem that I attach here.
    -sheet "list of substitutions" is where the values to be found/substituted and the replacement values are (column C and D)
    -sheet File 1 Microsoft is a raw example of how one of the 250 sheets are.
    -sheet File 1 Microsoft (corrected) is how the above should become.

    Any ideas?

    Much appreciated.

    Blackberry2012
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Find and replace in several (not all) sheets

    Hi blackberry2012

    Welcome to the forum...Give this a go...

    Option Explicit
    
    Sub ReplaceWords()
    Dim words, ws As Worksheet, i As Long
    Application.ScreenUpdating = False
    words = Sheet1.Range("C7:E" & Sheet1.Cells(Rows.Count, "C").End(xlUp).Row).Value
    For Each ws In ThisWorkbook.Sheets
            If ws.Name <> "List of Substitutions" Then
                With ws
                    For i = LBound(words, 1) To UBound(words, 1)
                        .Cells.Replace what:=words(i, 1), Replacement:=words(i, 3), _
                        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                        SearchFormat:=False, ReplaceFormat:=False
                    Next i
                End With
             End If
    Next ws
    Application.ScreenUpdating = True
    End Sub

    EDIT....
    in several (not all) sheets
    Just realised... What sheets in your actual book need to be ignored...These can be placed into an array...
    Option Explicit
    
    Sub ReplaceAll()
    Dim words, wsArr, ws As Worksheet, i As Long
    wsArr = Array("1", "2", "3") ' Sheets to be ignored....
    Application.ScreenUpdating = False
    words = Sheet1.Range("C7:E" & Sheet1.Cells(Rows.Count, "C").End(xlUp).Row).Value
    For Each ws In ThisWorkbook.Sheets
        If Not IsNumeric(Application.Match(ws.Name, wsArr, 0)) Then
            With ws
                For i = LBound(words, 1) To UBound(words, 1)
                    .Cells.Replace what:=words(i, 1), Replacement:=words(i, 3), _
                    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                    SearchFormat:=False, ReplaceFormat:=False
                Next i
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 04-23-2019 at 05:31 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Find and replace in several (not all) sheets

    Hi there,

    Take a look at the attached workbook and see if it does what you need.

    Click on the "List All Worksheets" button - this will create a table containing the names of all of the worksheets in the workbook. Then delete from this list the names of those worksheets on which you do NOT want Find And Replace operations performed.

    Then click on the "Find And Replace" button.


    The replacement value tables are divided into two types: Letter Case correction (for which only the final value need be specified) and Word correction (for which the initial and final values need be specified).


    The workbook uses the following code:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Const msSHEET_NAME__SUBSTITUTIONS   As String = "List of Substitutions"
    Const msSHEET_NAMES                 As String = "tblSheetNames"
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub ListAllWorksheets()
    
        Const sFIRST_CELL       As String = "ptrSheetNames_First"
    
        Dim iNoOfSheets         As Integer
        Dim rSheetNames         As Range
        Dim rFirstCell          As Range
        Dim sRefersTo           As String
        Dim iSheetNo            As Integer
        Dim wks                 As Worksheet
    
        Set wks = ThisWorkbook.Worksheets(msSHEET_NAME__SUBSTITUTIONS)
    
        iNoOfSheets = ThisWorkbook.Worksheets.Count
    
    '   Create a range which can hold the Names of each of the worksheets in this workbook
        Set rFirstCell = wks.Range(sFIRST_CELL)
        With rFirstCell
            Set rSheetNames = Range(.Cells(1, 1), _
                                    .Cells(iNoOfSheets, 1))
        End With
    
    '   Create a worksheet-level defined Name which references the above Range
        sRefersTo = "='" & msSHEET_NAME__SUBSTITUTIONS & "'!" & rSheetNames.Address
        wks.Names.Add Name:=msSHEET_NAMES, RefersTo:=sRefersTo
    
    '   Store the Names of each of the worksheets in the named range defined above
        For iSheetNo = 1 To iNoOfSheets
            rSheetNames.Cells(iSheetNo, 1).Value = ThisWorkbook.Worksheets(iSheetNo).Name
        Next iSheetNo
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub FindAndReplace()
    
        Const sCORRECTIONS__CASE    As String = "tblCaseCorrections"
        Const sCORRECTIONS__WORD    As String = "tblWordCorrections"
    
        Dim vaWorksheetNames        As Variant
        Dim wksSubstitutions        As Worksheet
        Dim rCaseCorrections        As Range
        Dim rWordCorrections        As Range
        Dim sValue_Replace          As String
        Dim sValue_Find             As String
        Dim sSheetName              As String
        Dim iSheetNo                As Integer
        Dim rCell                   As Range
        Dim wks                     As Worksheet
    
        Set wksSubstitutions = ThisWorkbook.Worksheets(msSHEET_NAME__SUBSTITUTIONS)
    
        Set rCaseCorrections = wksSubstitutions.Range(sCORRECTIONS__CASE)
        Set rWordCorrections = wksSubstitutions.Range(sCORRECTIONS__WORD)
    
        vaWorksheetNames = mvaWorksheetNames()
    
        For iSheetNo = LBound(vaWorksheetNames) To UBound(vaWorksheetNames)
    
            sSheetName = vaWorksheetNames(iSheetNo)
    
            Set wks = ThisWorkbook.Worksheets(sSheetName)
    
    '       Perform Letter Case corrections
            For Each rCell In rCaseCorrections.Cells
    
                sValue_Find = rCell.Value
    
                If sValue_Find <> vbNullString Then
    
                    sValue_Replace = rCell.Value
    
                    wks.UsedRange.Cells.Replace What:=sValue_Find, _
                                                Replacement:=sValue_Replace, _
                                                LookAt:=xlWhole, MatchCase:=False
    
                End If
    
            Next rCell
    
    '       Perform Word Case corrections
            For Each rCell In rWordCorrections.Columns(1).Cells
    
                sValue_Find = rCell.Value
    
                If sValue_Find <> vbNullString Then
    
                    sValue_Replace = rCell.Offset(0, 1).Value
    
                    wks.UsedRange.Cells.Replace What:=sValue_Find, _
                                                Replacement:=sValue_Replace, _
                                                LookAt:=xlWhole, MatchCase:=False
    
                End If
    
            Next rCell
    
        Next iSheetNo
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Function mvaWorksheetNames() As Variant
    
    '   This function returns an array containing the names of all of the worksheets
    '   on which Find And Replace operations are to be performed
    
        Dim vaSheetNames    As Variant
        Dim rSheetNames     As Range
        Dim rCell           As Range
        Dim wks             As Worksheet
    
        Set wks = ThisWorkbook.Worksheets(msSHEET_NAME__SUBSTITUTIONS)
    
        Set rSheetNames = wks.Range(wks.Names(msSHEET_NAMES))
    
        ReDim vaSheetNames(0)
    
        For Each rCell In rSheetNames.Cells
    
            If rCell.Value <> vbNullString Then
    
                If UBound(vaSheetNames) > 0 Then
    
                      ReDim Preserve vaSheetNames(1 To UBound(vaSheetNames) + 1)
                      vaSheetNames(UBound(vaSheetNames)) = rCell.Value
    
                Else: ReDim vaSheetNames(1 To 1)
                      vaSheetNames(1) = rCell.Value
    
                End If
    
            End If
    
        Next rCell
    
        mvaWorksheetNames = vaSheetNames
    
    End Function
    The highlighted value may be changed to suit your requirements.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    12-18-2012
    Location
    Almada
    MS-Off Ver
    Excel 2011
    Posts
    2

    Re: Find and replace in several (not all) sheets

    Thank you both! In the end both coding were precisely what I needed.

    Much a appreciated.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Find and replace in several (not all) sheets

    Pleasure...please mark thread as solved...
    Thanks.gif

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Find and replace in several (not all) sheets

    Hi again,

    Many thanks for your feedback.

    You're welcome - glad I was able to help.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find and Replace across Multiple Sheets
    By adambriggs in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 12-31-2014, 10:28 AM
  2. VBA How to find and replace all values over 100% with 100% in all sheets
    By XLOpenUse in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2014, 12:27 AM
  3. REQ: Help for a mass Find and Replace function between two sheets.
    By anonexcelquestion in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-10-2013, 01:16 PM
  4. [SOLVED] Find & Replace All Sheets Using VBA
    By tmontg2 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-28-2012, 04:06 PM
  5. Find and Replace in multiple sheets
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-11-2011, 05:29 PM
  6. Find and replace on all sheets
    By Cornwell in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-22-2010, 10:41 AM
  7. Find and Replace in multiple sheets
    By Excel Newbie05 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-06-2008, 04:34 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1