+ Reply to Thread
Results 1 to 20 of 20

Automating Find & Replace

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Question Automating Find & Replace

    I have a multi-sheet workbook that I use as a template for projects. A lot of the data is redundant, like a company's name or the project name, and it is inserted multiple times within the workbook. So to make it easy I thought of using a find/replace function. For example, when the document contains <project> it will replace it with the 'project name'. Then I decided to make a "KEY" which I call "code sheet" in the workbook that I use to enter in data then proceed to use the Find/Replace function to update the entire workbook based on the entry.

    However, while I thought this was smart in practice it feels even more redundant and time consuming because I have to copy/paste > find/replace each entry (column B) after i get done typing it in the 'code sheet'. I'd like to have it just do it on its own by referencing the code (column A) with whatever I type in the corresponding (column B).

    Problem is, I have ZERO knowledge of Macros or VB scripts. I don't even know where to start. Some direction would be much appreciated!
    Attached Files Attached Files
    Last edited by PharmerGuy; 05-01-2014 at 05:55 PM.

  2. #2
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    Sub Macro1()
    Call FindandReplace("Pepsi", "Coke")
    End Sub
    Private Sub FindandReplace(CompanyA As String, CompanyB As String)
    Dim Ws As Worksheet
    For Each Ws In ActiveWorkbook.Worksheets
        Cells.Replace What:=CompanyA, Replacement:=CompanyB, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False
    Next Ws
    
    End Sub
    This type of code might give you a starting point to build code.

  3. #3
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: Automating Find & Replace

    Thanks for the response. From the looks of this code it might not be deep enough and I need it to be much more global so the sheet stays dynamic if I were to add a new <code> from column A to automate across the sheets. Right now I have about 100 <code>s that I need to correlate a (replace) with.

  4. #4
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    So I decided to split up the Mastersheet from the MasterTemplate for ease of programming. In addition, on the Mastersheet I got rid of the empty rows because the Mastersheet is a table, and should be treated that way. There are ways to program around that, but I find that programming is much harder. In addition, Range Names act as arrays and the code is much easier to read.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: Automating Find & Replace

    Hello. Thanks for the files. But when I download both and try to hit "replace" on the MASTERSHEET I get the following error. both files are saved on my Desktop.

    Screen Shot 2014-05-05 at 12.11.32 PM.jpg

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Automating Find & Replace

    PharmerGuy,
    Why you don't use names!
    For example in sheet "Code Sheet" cell D2 is named "Company_address" then in sheet "Scope" in cell C8 put = Company_address
    PCI

  7. #7
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: Automating Find & Replace

    Quote Originally Posted by PCI View Post
    PharmerGuy,
    Why you don't use names!
    For example in sheet "Code Sheet" cell D2 is named "Company_address" then in sheet "Scope" in cell C8 put = Company_address
    PCI
    That would be simplest but there is multiple text in one cell. The replacement function is usually a set of words within a larger paragraph.

  8. #8
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Automating Find & Replace

    OK and I suppose you know you can concatenate strings like:
    = Project_Spensor & " wants CRM system to support both internal and external sales efforts – track clients, sales pipeline, probability of close, amount of contract, etc."
    Where Project_Sponsor stands for cell D7 in sheet "Code Sheet"
    Just to close the story with names

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Automating Find & Replace

    Try next code and check, check, check.
    Has been remove the space after the comma when 2 sheets where mentioned (Sheet1,Sheet2) to avoid confusion with the space inside the sheet's name
    Option Explicit
    
    Sub ReplaceData()
    Dim ObjDic   As Object
    Dim WkRg  As Range
    Dim F   As Range
    Dim G
    Dim WkSh As Worksheet
    Dim WkStg1 As String, WkStg2 As String
    Dim WkSet
    Dim I  As Integer
    Dim AAA, BBB, CCC
        Application.ScreenUpdating = False
        Set ObjDic = CreateObject("Scripting.Dictionary")
        With Sheets("Code Sheet")
            For Each F In Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
                If (F.Value <> "") Then ObjDic(F.Value) = F.Offset(0, 1).Value
            Next F
            For Each F In Range(.Range("E2"), .Range("E" & Rows.Count).End(xlUp))
                If (F.Value <> "") Then
                    If (F.Value = "All") Then
                        For Each WkSh In Worksheets
                            If (WkSh.Name <> "Code Sheet") Then
                                For Each G In ObjDic.keys
                                    WkStg1 = G
                                    Call FindReplace(WkStg1, ObjDic(G), WkSh.Name)
                                Next G
                            End If
                        Next WkSh
                    Else
                        WkSet = Split(F.Value, ",")
                        For I = 0 To UBound(WkSet, 1)
                            For Each G In ObjDic.keys
                                WkStg1 = G: WkStg2 = WkSet(I)
                                Call FindReplace(WkStg1, ObjDic(G), WkStg2)
                            Next G
                        Next I
                    End If
                End If
            Next F
        End With
        Application.ScreenUpdating = True
    End Sub
    
    Sub FindReplace(StgOrg As String, StgDest As String, WkSh As String)
        With Sheets(WkSh).Cells
            .Replace What:=StgOrg, Replacement:=StgDest, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
            ReplaceFormat:=False
        End With
    End Sub
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    Hi, the reason that you are having issue with that line of code is that the Template workbook is opened by the masterworkbook. If it starts closed than it will run.

    Thanks,

    Gregg

  11. #11
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: Automating Find & Replace

    Quote Originally Posted by greggpetersen75 View Post
    Hi, the reason that you are having issue with that line of code is that the Template workbook is opened by the masterworkbook. If it starts closed than it will run.

    Thanks,

    Gregg
    Hey Greg!

    I have both files in the same folder. When I open the MasterSheet and click on "replace code" I get a DEBUG error. It highlights that syntax I previously mentioned. The Project_Master_Template file is not open at this time.

  12. #12
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    You should not be getting an error if the MasterSheet.xlsm is open and Project_Master_Template.xlsx is closed and they are in the same folder. Did you change the name of the Project_Master_Template.xlsx in anyway?

    Thanks,

    Gregg

  13. #13
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: Automating Find & Replace

    Quote Originally Posted by greggpetersen75 View Post
    You should not be getting an error if the MasterSheet.xlsm is open and Project_Master_Template.xlsx is closed and they are in the same folder. Did you change the name of the Project_Master_Template.xlsx in anyway?

    Thanks,

    Gregg
    Hey Gregg,

    Sorry but I did not change a thing. When I click on the replace button I get this error. Note in the picture both files are on my Desktop and downloaded directly from this site as-is.

    Screen Shot 2014-05-07 at 10.31.23 AM.jpg

    If I click debug it takes me to the syntax line:

    Set TemplateWorkbook = Workbooks.Open(Filename:=ThisWbk.Path & "\Project_Master_Template.xlsx")

  14. #14
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Automating Find & Replace

    PharmerGuy,
    Did you ever try the macro I sent, just for the fun and my understanding please comment.
    PCI

  15. #15
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    You are running a Mac, I am sure that is what the issue is... I will come up with another solution when I return.

  16. #16
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: Automating Find & Replace

    Quote Originally Posted by greggpetersen75 View Post
    You are running a Mac, I am sure that is what the issue is... I will come up with another solution when I return.
    Good call, Gregg; common issue I'm told. And unfortunate.

  17. #17
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    Did you see my solution on page 2 of this thread?

  18. #18
    Registered User
    Join Date
    10-05-2012
    Location
    Chicago
    MS-Off Ver
    Excel 2011
    Posts
    9

    Re: Automating Find & Replace

    Quote Originally Posted by greggpetersen75 View Post
    Did you see my solution on page 2 of this thread?
    Hey Gregg,

    Still getting the error. I even tried to change the path so it read xlsm but no luck either way. I'm going to install Excel under Parallels tonight and get back to you to see if it works under that environment. Thank you for your hard work either way.

  19. #19
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    No I built a workbook where all the data stays in that workbook.

  20. #20
    Forum Contributor
    Join Date
    09-11-2013
    Location
    Huntsville, AL
    MS-Off Ver
    Excel 2007
    Posts
    180

    Re: Automating Find & Replace

    Here you go. Only thing that I "Hard coded" was that the worksheet name "Code Sheet" should keep that name. Also, I still would keep the "Code Sheet" in a table format with out any blank rows.

    Thanks,

    Gregg
    Attached Files Attached Files

+ 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 in excel replace in word: find/replace text in text boxes and headers
    By dean.rogers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2012, 12:40 PM
  2. Automating FIND AND REPLACE
    By jrodlc8 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-21-2011, 09:30 PM
  3. Automating Find and Replace
    By jcy1011 in forum Excel General
    Replies: 3
    Last Post: 07-30-2010, 05:52 PM
  4. Automating Find and Replace function.
    By gsmonk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2007, 02:13 PM
  5. [SOLVED] find and replace - replace data in rows to separated by commas
    By msdker in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-14-2006, 08:10 PM

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