+ Reply to Thread
Results 1 to 12 of 12

Macro find replace in formula

Hybrid View

  1. #1
    Registered User
    Join Date
    02-14-2015
    Location
    Paris, France
    MS-Off Ver
    2007
    Posts
    6

    Macro find replace in formula

    Hi,

    I am trying to run a macro replacing a part of a formula using find-replace:

    Find: AND(
    Replace: AND(INDIRECT(“AL”&ROW())="1");

    (within the macro of course quotation marks are added)

    When recording the macro this works perfectly fine, which is unfortunately not the case when I want to apply the macro in another file.

    Thanks a lot in advance if you have any advice on this!
    Lucie

    ----------------------------------------------------

  2. #2
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Macro find replace in formula

    Post your recorded code - don't forget code tags! We'll then edit the code for you so it works everywhere.
    Please consider adding a * if I helped.

  3. #3
    Registered User
    Join Date
    02-14-2015
    Location
    Paris, France
    MS-Off Ver
    2007
    Posts
    6

    Re: Macro find replace in formula

    Hi Brendan,

    Thanks for your fast reply. Please find below an essential part of the macro. (I only show this part of the macro as it is basically repeated a couple of times with slight adaptations).

    Sub Macro1()
        Sheets("RecapPro").Select
        Range("AO3:BL206").Select
        Selection.Replace What:="""AND(""", Replacement:= _
            "AND((INDIRECT(""AL""&ROW())=""1"");", LookAt:=xlPart, SearchOrder:= _
            xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    End Sub
    Thanks a lot in advance!
    Lucie

  4. #4
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Macro find replace in formula

    If you delete these 2 lines the macro will then work on what ever the selected cells are. The first line ties it to the sheet RecapPro the second line ties it to a specific range. So remove those 2 lines and it will work anywhere (assuming you've selected some cells)

     Sheets("RecapPro").Select
        Range("AO3:BL206").Select

  5. #5
    Registered User
    Join Date
    02-14-2015
    Location
    Paris, France
    MS-Off Ver
    2007
    Posts
    6

    Re: Macro find replace in formula

    Actually it is important to have the macro applied only to this specific sheet and range (because 'AND(' exists in other formulas where I don't want to have it replaced). Is that doable?

  6. #6
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Macro find replace in formula

    Ahh i see so you have lots of workbooks each with this sheet name and range?

    This below will loop through all open worksheets (in all workbooks) looking for sheets named RecapPro and perfom the find / replace

    
    Sub Macro1()
    Dim wks as worksheet
    Dim Wkb as workbook    
    
    for each wkb in workbooks
    for each wks in wkb.worksheets
      if wks.name = "RecapPro" then
        wks.Range("AO3:BL206").Replace What:="""AND(""", Replacement:= _
            "AND((INDIRECT(""AL""&ROW())=""1"");", LookAt:=xlPart, SearchOrder:= _
            xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
     end if
    next wks
    next wkb
    End Sub
    Last edited by Brendan_Floyde; 02-16-2015 at 09:32 AM. Reason: Now tested

  7. #7
    Registered User
    Join Date
    02-14-2015
    Location
    Paris, France
    MS-Off Ver
    2007
    Posts
    6

    Re: Macro find replace in formula

    Thanks a lot for the modified code! I tried it and it worked to a certain degree: it works if I simply write the to be replaced text into a cell, but the macro apparently does not access the formula itself and hence does not recognize what to replace within formulas. I have also tried switching the automatic recalculation of formulas to the manual mode and also tried the option of showing formulas, none of which helped either. Is there something else one could try?
    Many thanks again!

  8. #8
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Macro find replace in formula

    Ah sorry didn't even look at what you were trying to find and replace, assumed it worked . This is a bit more complicated as looks like you are trying to build a link based on location of cell. Can you post a desensitized example spreadsheet for us to play with?
    ta
    bren

  9. #9
    Registered User
    Join Date
    02-14-2015
    Location
    Paris, France
    MS-Off Ver
    2007
    Posts
    6

    Re: Macro find replace in formula

    Please find an example file attached here. I hope it is usable, else I will try to modify it. Thanks in advance!
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Macro find replace in formula

    As you want to build a replace string based on the cell found address you need to cycle through each found cell with findnext and build the string.

    Sub Macro1()
    Dim wks As Worksheet
    Dim Wkb As Workbook
    Dim lastcell As Range
    Dim seachrange As Range
    Dim cellfound As Variant
    
    Dim firstaddress As String
    
    Dim findtext As String
    Dim replacetext As String
    
    findstring = "AND("
    
    For Each Wkb In Workbooks
    For Each wks In Wkb.Worksheets
      If wks.Name = "RecapPro" Then
        
        Set searchRange = wks.Range("AO3:BL206")
        Set lastcell = searchRange.Cells(searchRange.Cells.Count)
        
        Set cellfound = searchRange.Find(What:=findstring, After:=lastcell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    
    If Not cellfound Is Nothing Then
    firstaddress = cellfound.Address
    
     
    Do
    
    Set cellfound = searchRange.FindNext(cellfound)
    replacetext = "AND(INDIRECT(" & Chr(34) & "AL" & cellfound.Row & Chr(34) & " )=1,"
    
    cellfound.Formula = Replace(cellfound.Formula, findstring, replacetext)
    
    Loop Until cellfound.Address = firstaddress
      
    End If
    
     End If
    Next wks
    Next Wkb
    
    Set lastcell = Nothing
    Set seachrange = Nothing
    Set cellfound = Nothing
    
    End Sub
    Last edited by Brendan_Floyde; 02-18-2015 at 08:18 PM.

  11. #11
    Registered User
    Join Date
    02-14-2015
    Location
    Paris, France
    MS-Off Ver
    2007
    Posts
    6

    Re: Macro find replace in formula

    Excellent, that's exactly what I needed! Thank you so much!
    All the best,
    Lucie

  12. #12
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Macro find replace in formula

    If you are happy please can you mark your thread solved?
    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved


    and if I helped please consider some * add rep.

    ta
    bren

+ 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. Replies: 3
    Last Post: 11-25-2014, 06:08 AM
  2. [SOLVED] Macro to Find & Replace existing formula with text in referenced cells
    By sonyap in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2013, 10:39 AM
  3. [SOLVED] Find and Replace Macro not working for sheet Referance in formula
    By Lotrking1010 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-31-2012, 08:15 AM
  4. Find/Replace Macro Using Replace Table
    By LampCommandr in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-24-2011, 11:00 AM
  5. [SOLVED] Using Find and Replace to replace " in a macro
    By snail30152 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2006, 06:58 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