+ Reply to Thread
Results 1 to 14 of 14

Find & Replace All Sheets Using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Find & Replace All Sheets Using VBA

    Thanks in advance!

    I need to find column a and replace with column B text across all sheets. The code I have is only doing the find and replace on one sheet? Any tips?

    Sub SearchReplaceMacro()
    
        Cells.Replace What:=Range("A1").Text, Replacement:=Range("B1").Text, LookAt:=xlWhole, _
            SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A2").Text, Replacement:=Range("B2").Text, LookAt:=xlWhole, _
            SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A3").Text, Replacement:=Range("B3").Text, LookAt:=xlWhole, _
            SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A4").Text, Replacement:=Range("B4").Text, LookAt:=xlWhole, _
            SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A5").Text, Replacement:=Range("B5").Text, LookAt:=xlWhole, _
            SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A6").Text, Replacement:=Range("B6").Text, LookAt:=xlWhole, _
            SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A7").Text, Replacement:=Range("B7").Text, LookAt:=xlWhole, _
            SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    
    End Sub

  2. #2
    Registered User
    Join Date
    05-26-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Find & Replace All Sheets Using VBA

    Correction on Code..

    
    Option Explicit
    
    Sub SearchReplaceMacro()
    
        Cells.Replace What:=Range("A1").Text, Replacement:=Range("B1").Text, LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A2").Text, Replacement:=Range("B2").Text, LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A3").Text, Replacement:=Range("B3").Text, LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A4").Text, Replacement:=Range("B4").Text, LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A5").Text, Replacement:=Range("B5").Text, LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A6").Text, Replacement:=Range("B6").Text, LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells.Replace What:=Range("A7").Text, Replacement:=Range("B7").Text, LookAt:=xlWhole, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    03-21-2012
    Location
    Ho Chi Minh city
    MS-Off Ver
    Excel 2003
    Posts
    180

    Re: Find & Replace All Sheets Using VBA

    Option Explicit
    Sub AhhSheetsSearchReplace()
     Dim Sh As Worksheet
     For Each Sh In ThisWorkbook.Worksheets
        With Sh
            .Cells.Replace What:=.Range("A1").Text, Replacement:=.Range("B1").Text, LookAt:=xlWhole, _
                SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            .Cells.Replace What:=.Range("A2").Text, Replacement:=.Range("B2").Text, LookAt:=xlWhole, _
                SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            .Cells.Replace What:=.[A3].Text, Replacement:=Sh.[B3].Text, LookAt:=xlWhole, _
                SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            .Cells.Replace What:=.[A4].Text, Replacement:=.[B4].Text, LookAt:=xlWhole, _
                SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            .Cells.Replace What:=.[A5].Text, Replacement:=.[B5].Text, LookAt:=xlWhole, _
                SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            .Cells.Replace What:=.[A6].Text, Replacement:=.[B6].Text, LookAt:=xlWhole, _
                SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            .Cells.Replace What:=.[A7].Text, Replacement:=.[B7].Text, LookAt:=xlWhole, _
                SearchOrder:=xlPart, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        End With
     Next Sh
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Find & Replace All Sheets Using VBA

    Sub SearchReplaceMacro()
     Dim sh As Worksheet
     For Each sh In ActiveWorkbook.Worksheets
      With sh
       .Range("A1:A7") = .Range("B1:B7").Value
      End With
     Next sh
    End Sub
    Last edited by HSV; 05-28-2012 at 05:45 AM. Reason: Edit the wrong code
    Harry.

  5. #5
    Registered User
    Join Date
    05-26-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Find & Replace All Sheets Using VBA

    Thanks! That worked faster. However it is only finding and replacing in row A and row B across all sheets... I want it search entire worksheet for text shown in "row A" and replace any instance of it with the corresponding text in "row B" . I do not want it to match entire cell content.

    Is that possible?

  6. #6
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Find & Replace All Sheets Using VBA

    Sub hsv()   
    Dim sh As Worksheet, i As Long
     For Each sh In ThisWorkbook.Worksheets
       For i = 1 To 7
        sh.Cells.Replace sh.Cells(i, 1), sh.Cells(i, 2), xlWhole, xlByRows, , False, False, False
       Next i
     Next sh
    End Sub
    Last edited by HSV; 05-28-2012 at 05:42 AM.

  7. #7
    Registered User
    Join Date
    05-26-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Find & Replace All Sheets Using VBA

    getting error.. Capture.PNG

  8. #8
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Find & Replace All Sheets Using VBA

    I have a file imitated, but no error to me.
    Maybe you could upload your file so we can see where things go wrong.

  9. #9
    Registered User
    Join Date
    05-26-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Find & Replace All Sheets Using VBA

    Quote Originally Posted by HSV View Post
    I have a file imitated, but no error to me.
    Maybe you could upload your file so we can see where things go wrong.
    Here ya go! And thanks again.

  10. #10
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Find & Replace All Sheets Using VBA

    Your length of your words may have a length to 255 characters.
    Sheet 1 cell B6 has 461 characters.

  11. #11
    Registered User
    Join Date
    05-26-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Find & Replace All Sheets Using VBA

    Quote Originally Posted by HSV View Post
    Your length of your words may have a length to 255 characters.
    Sheet 1 cell B6 has 461 characters.
    Shortened it and it still is only replacing rows A & B only, its not finding instances of A in entire sheet and replacing all instances of A with B... lol. I've been trying to get this to work for 2 days.

  12. #12
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Find & Replace All Sheets Using VBA

    Replace by 'xlwhole' 'xlpart'

  13. #13
    Registered User
    Join Date
    05-26-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Find & Replace All Sheets Using VBA

    Quote Originally Posted by HSV View Post
    Replace by 'xlwhole' 'xlpart'
    THANK YOU!!!!!!!!!!!!

    I could give you hug!

    -Tierra

  14. #14
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Find & Replace All Sheets Using VBA

    You’re welcome,

    Make sure columns B not blank cells in it.
    Otherwise columns A will be a blank cell to.
    Change the code to overcome.
    Sub hsv()
    Dim sh As Worksheet, i As Long
     For Each sh In ThisWorkbook.Worksheets
       For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        If sh.Cells(i, 2) > 0 Then
         sh.Cells.Replace sh.Cells(i, 1), sh.Cells(i, 2), xlPart, xlByRows, , False, False, False
        End If
       Next i
     Next sh
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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