+ Reply to Thread
Results 1 to 2 of 2

Find and Replace Loop does not find all and loop properly

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2015
    Location
    Melbourne,Australia
    MS-Off Ver
    2007
    Posts
    14

    Find and Replace Loop does not find all and loop properly

    Hi,

    The below code is meant to find special characters and replace them. I am using it in a worksheet that has ~300 rows and columns A-X.

    It seems to pick up some special characters and replace them but not all of them? If I run the macro again it will find more but again not all, and basically I have to keep running and manually check until it finds them all. Yet it does not report any errors? Please help I can't figure this one out.

    Sub SpecialCharactersCheck()
    
        Dim fndList As Variant
        Dim rplcList As Variant
        Dim rFound As Range
        Dim response As VbMsgBoxResult
        Dim x As Long
        Dim vFile1 As Variant
        Dim newWB As Workbook
        Dim newWBS As Worksheet
        
        'Message box asking user to select the appropriate workbook
        MsgBox ("Please select the workbook containing the Worksheet: ValidationRules-Common")
                
        'Open the target workbook
        vFile1 = Application.GetOpenFilename("Excel-files,*.xls*", _
        1, "Select document to check for special characters", , False)
    
        'If the user didn't select a file, exit sub
        If TypeName(vFile1) = "Boolean" Then Exit Sub
        
        'Set the workbook and worksheet
        Set newWB = Workbooks.Open(vFile1)
        Set newWBS = newWB.Worksheets("ValidationRules-Common")
        
        'Search all cells in the ValidationRules-Common sheet
        For Each cell In ActiveSheet.UsedRange.Cells
        
            fndList = Array("‘", "–", "’", "“", "”")
            rplcList = Array("'", "-", "'", """", """")
            
            Do
                For x = LBound(fndList) To UBound(fndList)
                    'Reset the found variable and search
                    Set rFound = Nothing
                    Set rFound = newWBS.Cells.Find(What:=fndList(x), LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                    SearchFormat:=False)
                    
                    'Check if a special character was found.
                    If Not rFound Is Nothing Then
                        response = MsgBox("The special character: " & fndList(x) & " was found in cell " & rFound.Address(0, 0) & ". Do you want to replace it?", vbInformation + vbYesNoCancel)
                        If response = vbYes Then
                            rFound.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
                            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
                            SearchFormat:=False, ReplaceFormat:=False
                            rFound.Cells.Interior.ColorIndex = 6
                            'rFound.Cells.Characters.Font.Color = vbRed
                        End If
                        If response = vbNo Then
                            MsgBox ("Skipping Character change")
                        End If
                        If response = vbCancel Then
                            MsgBox ("Macro stopped, there may still be special characters in the worksheet")
                            Exit Sub
                        End If
                    End If
                    
                Next x
            'Loop until no more special characters are found
            Loop Until rFound Is Nothing
            MsgBox ("Macro finished")
            Exit Sub
        Next
    End Sub
    Thanks,
    Mattyfaz

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Find and Replace Loop does not find all and loop properly

    Your structure is a little unusual, at least to me.
        'Search all cells in the ValidationRules-Common sheet
        For Each cell In ActiveSheet.UsedRange.Cells
    I don't know why you would even use this. You would specify this range as the range that range.find would search and that would be all you need. So I would get rid of this loop. You then have a do loop outside of your for x loop which also is a little strange. I would remove the do loop and then I would structure the code as follows.

    You only need to loop through your findList one time. Instead of using just range.find use range.find followed by find next and loop the find next until it equals nothing and then loop x and do it again. That way you are looking for all the first characters in your array list until it finds no more then the second and so on. Also when you use find next, if you code it correctly it starts where you left off, it just continues from there looking for another match. You check if it's nothing if it is you exit the find next loop and go to the next x until your array has been looped through and you are done.

    Good Luck.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

+ 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. [SOLVED] Loop - Find and Replace
    By ELeGault in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-10-2015, 06:45 PM
  2. Find & Replace Loop Help Needed
    By bryanmarks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2014, 10:19 AM
  3. [SOLVED] Find String Loop not Terminating Properly
    By Doug Nguyen in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-18-2014, 11:02 AM
  4. 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
  5. Excel 2007 : Find and replace loop
    By chrisrabkin in forum Excel General
    Replies: 1
    Last Post: 10-18-2011, 05:58 PM
  6. Find and replace Loop Error
    By fengfeng in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2009, 03:57 PM
  7. 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