+ Reply to Thread
Results 1 to 11 of 11

Loop - Find and Replace

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Loop - Find and Replace

    I am trying to make a find and replace that will find ABS which is any time ABS() is used and replace it with a relative formula ...
    I tried using the find and replace to replace all...which works but not in tandem with R1C1 style formulas.

    Sub FReplace()
        Cells.Replace What:="*ABS*", Replacement:= _
            "=IFERROR(IF(AND(RC[-6]=0,RC[-5]<>0),1,ABS(RC[-5]/RC[-6])),0)", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    End Sub
    So my next thought was a loop... as making it the found cell active first and doing a replace or entering the formula will work and have it go until it is past the last row count...
    Could not get a loop to work but here is a code that works, and replaces the next cell in order in which they appear but requires hitting play over and over... I have thousands in various columns to replace...

    Sub FReplaceActive()
        Cells.Find(What:="*ABS*", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        ActiveCell.FormulaR1C1 = _
            "=IFERROR(IF(AND(RC[-6]=0,RC[-5]<>0),1,ABS(RC[-5]/RC[-6])),0)"
    End Sub

    Fingers are crossed -

    Cheers
    -If you think you are done, Start over - ELeGault

  2. #2
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: Loop - Find and Replace

    Currently on all sheets - there are several columns - never goes below 300 Rows! so I tried Do While Rows.Count < 301 but that seemed to not work... currently sitting with the module open and setting a water bottle on a pen that is holding F5 down while it runs through each sheet... lol - has to be a better way

  3. #3
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Loop - Find and Replace

    Total guess here but rather than use cells and step through each one separately can you use this syntax

    Worksheets("Sheet1").Columns("A").find

    Instead
    Happy with my advice? Click on the * reputation button below

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: Loop - Find and Replace

    You didn't post the loop code. You should be using something like the code from HELP:

    Example
    This example finds all cells in the range A1:A500 on
    worksheet one that contain the value 2 and changes it to 5.
     
    With Worksheets(1).Range("a1:a500")
        Set c = .Find(2, lookin:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Value = 5
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
    If you have multiple sheets and multiple columns in each sheet, you probably need a couple of For Each... loops to select the sheet then select the column(s) within that sheet the finally the code above. (3 level nesting)
    Last edited by protonLeah; 03-06-2015 at 10:37 PM.
    Ben Van Johnson

  5. #5
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: Loop - Find and Replace

    Not at home - but will try to wrap it up in something similar - My loop code was garbage so figured I would show the replace method hoping for a suggestion - will play with that code and let you know what I come up with and/or if I hit errors

    Cheers

  6. #6
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: Loop - Find and Replace

    So finally got around to this after a weekend of hell dealing with a Transmission...

    This is the final (Working) for what I need - though occasionally once finished I get an error
    Object Variable or With block not set
    Not entirely sure what I am missing... I added an extra set C = Nothing just to clear it but even still happens (Not always and the code finishes so... idk?)

    protonLeah, used your quoted with statement - Thanks for that!

    Sub FReplace_S()
    
        Dim WS_Count As Integer
        Dim I As Integer
        Dim lastRow As Long
        Dim lastColumn As Integer
        Dim EndCol As String
        Dim FindWhat As String
        Dim ReplaceWithWhat As String
       
        FindWhat = "*ABS*"
        ReplaceWithWhat = "=IFERROR(IF(AND(RC[-6]=0,RC[-5]<>0),1,ABS(RC[-5]/RC[-6])),0)"
        
        lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
        lastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
        EndCol = Replace(Cells(1, lastColumn).Address(False, False), "1", "")
    
        WS_Count = ActiveWorkbook.Worksheets.Count
        
        For I = 1 To WS_Count
    
            With Worksheets(I).Range("A1:" & EndCol & lastRow)
                Set c = .Find(FindWhat, LookIn:=xlFormulas)
                If Not c Is Nothing Then
                    firstAddress = c.Address
                    Do
                        c.Value = ReplaceWithWhat
                        Set c = .FindNext(c)
                    Loop While Not c Is Nothing And c.Address <> firstAddress
                End If
            End With
        
        Next I
    
        Set c = Nothing
    
    End Sub
    Will leave it open for another day for error troubleshooting - otherwise - protonLeah and Crooza - Appreciate the assist!
    Last edited by ELeGault; 03-09-2015 at 02:23 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: Loop - Find and Replace

    So looks like that block error is an issue - Open to suggestions on where I am going wrong -

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,282

    Re: Loop - Find and Replace

    This:
    Loop While Not c Is Nothing And c.Address <> firstAddress
    is actually incorrect, even though it's what the Help files have always shown. Since both conditions are evaluated for And, if c is Nothing, trying to access c.Address will raise an error.

    Given that you are replacing one string containing "ABS" with another string containing "ABS", c shouldn't ever be Nothing inside the loop (though you should be using the .FormulaR1C1 property, not the .Value). How reproducible is the error?
    Remember what the dormouse said
    Feed your head

  9. #9
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: Loop - Find and Replace

    Extremely reproducible - It happens with another one I attempted to do which was Findwhat= " $L35 " and ReplaceWithWhat was another formula which would not have had that locked reference in it.

    It happens every time the condition is met and the replace takes effect... You can add the code to a blank workbook... add something in any cell then make sure the findwhat is able to locate in that book...

    Open for suggestions...

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,282

    Re: Loop - Find and Replace

    If you are replacing all instances with another string that does not contain the original search string, you should only use:
    Loop While Not c Is Nothing
    If you are replacing with a string that contains the same text, you should only use:
    Loop While c.Address <> firstAddress

  11. #11
    Valued Forum Contributor
    Join Date
    11-22-2014
    Location
    Arizona, USA
    MS-Off Ver
    Office 365
    Posts
    973

    Re: Loop - Find and Replace

    Works with Loop While Not c Is Nothing, seem to get the same block error with the Loop While C.Address <> firstAddress

    So as long as I do not attempt to replace with similar string this is good to go - Thanks RomperStomper!

+ 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 & Replace Loop Help Needed
    By bryanmarks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2014, 10:19 AM
  2. VBA Find and replace loop help needed
    By susanbarbour in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2012, 08:43 AM
  3. Excel 2007 : Find and replace loop
    By chrisrabkin in forum Excel General
    Replies: 1
    Last Post: 10-18-2011, 05:58 PM
  4. Find and replace Loop Error
    By fengfeng in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2009, 03:57 PM
  5. Find & Replace / Loop & Vlookup
    By thom hoyle in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-24-2005, 08:05 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