Consulting

Results 1 to 4 of 4

Thread: Exit Do after search item no longer found in a range

  1. #1
    VBAX Contributor
    Joined
    Nov 2012
    Location
    Billericay, Essex
    Posts
    145
    Location

    Exit Do after search item no longer found in a range

    Hi,
    I have a short loop that finds an account number in column B and then changes the value in column D in that row
    my problem is once it no longer finds the value in the last rows it crashes - so I need an exit loop command.
    Please see attached, this is my code so far:

    Sub Test()  
        'Change UNALC to DESK for A0246074TRA
        
        Range("B2").Select
    Do
    Range(ActiveCell, ActiveCell.End(xlDown)).Select
        Selection.Find(What:="A0246074TRA", After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset(0, 2).Select
        ActiveCell.FormulaR1C1 = "DESK"
        ActiveCell.Offset(1, -2).Select
        Range(ActiveCell, ActiveCell.End(xlDown)).Select
      
    Loop
     
     
     End Sub
    Any help as always is much appreciated.
    Change Test.xlsm
    Last edited by pcarmour; 07-13-2016 at 07:58 AM. Reason: Attachment
    Regards, Peter.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,732
    Location
    1. you don't need to .Select things to work with them

    2. Your Do Loop just keeps on running. Usually there's a While or Until or some kind of internal 'Exit Do' condition

    3. I think you can make it simpler (I think this is what you wanted to do)


    Option Explicit
    Sub Test1()
        Dim R As Range
        
        Application.ScreenUpdating = False
        For Each R In ActiveSheet.Cells(1, 1).CurrentRegion.Rows
            If Trim(R.Cells(1, 2).Value) = "A0256065TRA" Then   ' your cells have trailing spaces!!!!!
                R.Cells(1, 4).Value = "DESK"
            End If
        Next
        Application.ScreenUpdating = True
     
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Contributor
    Joined
    Nov 2012
    Location
    Billericay, Essex
    Posts
    145
    Location
    Fantastic, does exactly what I wanted, thank you very much Paul.
    Regards, Peter.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For your information, here is the "standard" FindNext example from Excel help files which exits the loop
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •