+ Reply to Thread
Results 1 to 10 of 10

Loop with selection.delete and InStr is ending early

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    Guelph, ON
    MS-Off Ver
    Excel 2010
    Posts
    41

    Loop with selection.delete and InStr is ending early

    Hello: I can not for the life of me figure this out. I have even switched the loop to go from Last row to 2 (starting from bottom up) and no matter what I do my loop ends before it is done. I'm not sure if it has to do with the delete nature of the command?

    Background: I'm cleaning up a large set of data. I've provided a small sample to illustrate my problem.

    The first loop appears to work flawlessly. The first loop scrolls through and shifts any cell containing ":" to the right. Next I want to scroll through Column C and remove and shift cells to the left for cells with text containing "photo". This second loop never makes it to the end. I have tried re-defining the last row, ranges, etc. Would anyone have any brilliant ideas?

    I've attached the sample spreadsheet for you with the VBA code.

    Original sample data is on one tab and you can see where the code ends in Column C on the Cleaned Up data tab.

    Hopefully this makes sense.

    Thanks,
    Stef,
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,097

    Re: Loop with selection.delete and InStr is ending early

    This works:

    Sub CleanUp_TMS()
    
    Dim lastrow As Long
    Dim cel As Range
    
    'this will move region to column b and leave a 2nd location if there is one in column a
    lastrow = Range("A" & rows.Count).End(xlUp).Row
    
    Debug.Print Range("a2:a" & lastrow).Address
    For Each cel In Range("a2:a" & lastrow)
        Debug.Print cel.Address, cel.Value
        If InStr(cel.Value, ":") > 0 Then
            cel.Insert Shift:=xlToRight
        End If
    Next cel
     
    'remove photo lines out of Column C
    Debug.Print Range("c2:c" & lastrow).Address
    For Each cel In Range("C2:C" & lastrow)
        Debug.Print cel.Address, cel.Value
        If InStr(cel.Value, "photo") > 0 Then
            cel.Delete Shift:=xlToLeft
        End If
    Next cel
    
    End Sub
    EXCEPT ... I am at a loss to explain why cell C3 is skipped in the second loop.

    $A$2:$A$18
    $A$2 Hamilton : Hamilton-Wentworth
    $A$3 Hamilton : Hamilton-Wentworth
    $A$4 Dundas : Hamilton-Wentworth
    $A$5 Stoney Creek : Hamilton-Wentworth
    $A$6 Hamilton : Hamilton-Wentworth
    $A$7 425 WILSON ST E
    $A$8 Flamborough : Hamilton-Wentworth
    $A$9 Dundas : Hamilton-Wentworth
    $A$10 Flamborough : Hamilton-Wentworth
    $A$11 135 HWY 8
    $A$12 UNITS C11 & C1A
    $A$13 Hamilton : Hamilton-Wentworth
    $A$14 Dundas : Hamilton-Wentworth
    $A$15 Hamilton : Hamilton-Wentworth
    $A$16 Hamilton : Hamilton-Wentworth
    $A$17 Hamilton : Hamilton-Wentworth
    $A$18 Hamilton : Hamilton-Wentworth
    $C$2:$C$18
    $C$2 street photos:*1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20*21*22*23*24*25*26*27
    $C$4 street photos:*1*2*3*4*5*6*7
    $C$5 street photos:*1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18
    $C$6 street photos:*1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17
    $C$7
    $C$8 street photos:*1*2*3*4*5*6*7
    $C$9 street photos:*1*2*3*4*5*6*7
    $C$10 street photos:*1*2*3*4*5*6
    $C$11
    $C$12
    $C$13 Transferor(s)
    $C$14 street photos:*1*2*3*4*5
    $C$15 street photos:*1*2*3*4*5
    $C$16 street photos:*1*2*3*4*5
    $C$17 street photos:*1*2*3*4*5
    $C$18 Transferor(s)
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,222

    Re: Loop with selection.delete and InStr is ending early

    Seem like deleting the first cell C2 on range "C2:C18" cause the C3 to skip
    Change C2 to C1 fix it


    Sub CleanUp_TMS()
    
    Dim lastrow As Long
    Dim cel As Range
    
    'this will move region to column b and leave a 2nd location if there is one in column a
    lastrow = Range("A" & rows.Count).End(xlUp).Row
    
    Debug.Print Range("a2:a" & lastrow).Address
    For Each cel In Range("a2:a" & lastrow)
        Debug.Print cel.Address, cel.Value
        If InStr(cel.Value, ":") > 0 Then
            cel.Insert Shift:=xlToRight
        End If
    Next cel
     
    'remove photo lines out of Column C
    Debug.Print Range("c2:c" & lastrow).Address
    For Each cel In Range("C1:C" & lastrow)
        Debug.Print cel.Address, cel.Value
        If InStr(cel.Value, "photo") > 0 Then
            cel.Delete Shift:=xlToLeft
        End If
    Next cel
    
    End Sub

    or try

    PHP Code: 
    Sub cUp()

    Dim ai&
    = [a1].CurrentRegion.Value
    For 1 To UBound(a)
        If 
    InStr(a(i1), ":"Then
            
    If InStr(a(i2), "photo") = 0 Then
                a
    (i4) = a(i3)
                
    a(i3) = a(i2)
            
    End If
            
    a(i2) = a(i1)
            
    a(i1) = Empty
        
    End If
    Next
    [a1].CurrentRegion.Value a
    End Sub 
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    10-24-2012
    Location
    Guelph, ON
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Loop with selection.delete and InStr is ending early

    Thank you so very much!!! Both of your solutions worked. I understand the first one better though even though it is longer or more typing! hehe But honestly... so appreciated. I need to learn to ask here sooner than bang my head and struggle for hours.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,525

    Re: Loop with selection.delete and InStr is ending early

    From my tests, it appears that a reference to the range C2: C18 (in the test - the variable Rng) behaves differently. When the first or last cell of a range is deleted, this reference changes its address. However, if cells from the second to the penultimate one are deleted - the reference remains unchanged. I cannot explain why removing cells at the borders changes the address, and removing cells inside does not.
    Macro for testing only:
    Sub CleanUp_Test_Art()
    
        Dim lastrow     As Long
        Dim cel         As Range
        Dim Rng         As Range
    
        'this will move region to column b and leave a 2nd location if there is one in column a
        lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
        Set Rng = Range("A2:A" & lastrow)
        Debug.Print "Rng.Address: " & Rng.Address
        
        For Each cel In Rng
            Debug.Print cel.Address(0, 0) & " | " & IIf(InStr(cel.Value, ":") > 0, "To insert", "It remains")
            
            If InStr(cel.Value, ":") > 0 Then
                cel.Insert Shift:=xlToRight
            End If
            
            Debug.Print IIf(InStr(cel.Value, ":") > 0, "After insert: ", "After: ") & cel.Address(0, 0)
            Debug.Print "Rng.Address: " & Rng.Address(0, 0)
        Next cel
    
        Debug.Print String(10, "- ")
    
        'remove photo lines out of Column C
        Set Rng = Range("C2:C" & lastrow)
        Debug.Print "Rng.Address: " & Rng.Address
        
        On Error Resume Next
        
        For Each cel In Rng
            Err.Clear
            Debug.Print cel.Address(0, 0) & " | " & IIf(InStr(cel.Value, ":") > 0, "To delete", "It remains")
            If InStr(cel.Value, "photo") > 0 Then
                cel.Delete Shift:=xlToLeft
            End If
            Debug.Print "After: " & cel.Address(0, 0)
            If Err.Number <> 0 Then
                Debug.Print Err.Description
            End If
            Debug.Print "Rng.Address: " & Rng.Address(0, 0)
        Next cel
    
    End Sub

    Another solution to the problem is to use a descending loop:
    Sub CleanUp_Art2()
    
        Dim lastrow     As Long
        Dim cel         As Range
        Dim Rng         As Range
        Dim i           As Long
    
        'this will move region to column b and leave a 2nd location if there is one in column a
        lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
        Set Rng = Range("A2:A" & lastrow)
        
        For Each cel In Rng
            If InStr(cel.Value, ":") > 0 Then
                cel.Insert Shift:=xlToRight
            End If
        Next cel
        
        'remove photo lines out of Column C
        For i = lastrow To 2 Step -1
            If InStr(Cells(i, "C").Value, "photo") > 0 Then
                Cells(i, "C").Delete Shift:=xlToLeft
            End If
        Next i
    
    End Sub
    Artik

  6. #6
    Registered User
    Join Date
    10-24-2012
    Location
    Guelph, ON
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Loop with selection.delete and InStr is ending early

    Thank you also for taking time to look at this. I had also tried the descending loop but it didn't work for me either. It was a headscratcher for sure! Thanks so much for the help!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,097

    Re: Loop with selection.delete and InStr is ending early

    @Bo_Ry/Artik: thanks for the workarounds. I am still intrigued that the "fail" is inconsistent. If the loop had missed other cells, I could understand it to a certain extent and then the traditional workaround of working from the bottom up would make more sense. But just one cell near the top of the loop is very confusing.

    Anyway, my workaround

    Option Explicit
    
    Sub CleanUp_TMS_v2()
    
    Dim lastrow As Long
    Dim cel As Range, rDel As Range
    
    'this will move region to column b and leave a 2nd location if there is one in column a
    lastrow = Range("A" & rows.Count).End(xlUp).Row
    
    Debug.Print Range("a2:a" & lastrow).Address
    For Each cel In Range("a2:a" & lastrow)
        Debug.Print cel.Address, cel.Value
        If InStr(cel.Value, ":") > 0 Then
            cel.Insert Shift:=xlToRight
        End If
    Next cel
     
    'remove photo lines out of Column C
    Debug.Print Range("c2:c" & lastrow).Address
    For Each cel In Range("C2:C" & lastrow)
        Debug.Print cel.Address, cel.Value
        If InStr(cel.Value, "photo") > 0 Then
            If rDel Is Nothing Then
                Set rDel = cel
            Else
                Set rDel = Union(rDel, cel)
            End If
        End If
    Next cel
    
    If Not rDel Is Nothing Then rDel.Delete Shift:=xlToLeft
    
    End Sub

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,097

    Re: Loop with selection.delete and InStr is ending early

    Not sure which solution you opted for. My first solution was close but, for some reason, missed a cell out in the second loop. That might have been masked in your live file. My second solution works ok.

    I'm guessing the solutions offered by Bo_Ry and Artik worked but I didn't test them.

    You don't need to type any solutions; you can copy and paste them.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  9. #9
    Registered User
    Join Date
    10-24-2012
    Location
    Guelph, ON
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Loop with selection.delete and InStr is ending early

    Thanks for the tip on the star!! I didn't notice that before. You all rock in here!

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,097

    Re: Loop with selection.delete and InStr is ending early

    You're welcome. Thanks for the rep.

+ 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] instr function if loop to find data
    By diyVBA in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 05-06-2020, 11:20 AM
  2. [SOLVED] Macro Ending Early - When Using Range.Value on some macros?
    By bkm2016 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-28-2017, 04:41 PM
  3. Loop finishing too early!!
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-16-2013, 01:23 PM
  4. [SOLVED] How to break out of a For loop early?
    By geophysicist in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-12-2013, 11:08 PM
  5. For Exit Loop in instr function
    By drcheaud in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2011, 03:24 PM
  6. Ending macro early
    By TedH in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-20-2009, 11:45 AM
  7. Ending a macro early conditionally on one cell being blank
    By Rokuro kubi in forum Excel General
    Replies: 3
    Last Post: 05-26-2006, 09:15 AM

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