+ Reply to Thread
Results 1 to 3 of 3

VBA Loop Macro Fix for search code

Hybrid View

  1. #1
    Registered User
    Join Date
    08-06-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    80

    VBA Loop Macro Fix for search code

    I've a problem with some code, I think this is running on an eternal loop but can't figure this out. Please can someone have a look at let me know what they think the problem is. It's a simple lookup and loop code but I seem to have got it wrong. I've now been staring at it for the past two days and I'm flying the white flag.

    Sub StoreNumbers()
    
    
    Dim FoundCell As Range
    Dim lastCell As Range
    Dim FirstAddr As String
    Dim a As Range 'List of stores
    Dim b As Range 'List of store numbers
    Dim c As Range 'Conversation headers
    
    
    
    For Each a In Worksheets("Reference").Range("L2:L1000")
    
    Set c = Sheets("Recieved").Range("C2:C1000")
    Set lastCell = c.Cells(c.Cells.Count)
    Set FoundCell = c.Find(what:=a, after:=lastCell, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, _
    searchdirection:=xlNext)
    
    If Not FoundCell Is Nothing Then
    FirstAddr = FoundCell.Address
    Do
    Set FoundCell = c.FindNext(FoundCell)
    
    FoundCell.Offset(0, 12).Value = a
    
    Loop Until FoundCell.Address = FirstAddr
    End If
    
    Next
    
    
    
    End Sub

    Thanks in advance

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Loop Macro Fix

    Why this -
    Set lastCell = c.Cells(c.Cells.Count)
    You can use
    after:=sheets("Received").range("A1")
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    08-06-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: VBA Loop Macro Fix

    With

    Set lastCell = c.Cells(c.Cells.Count)
    It should allow the the code to figure out how long it needs to be. I orignally thought this may have been the issue as well but this column is simply text based so there's no forumlae to interfer with this count. I'll have a play about with your suggestion and will let you know the outcome.

    ---------- Post added at 12:44 PM ---------- Previous post was at 12:37 PM ----------

    Ok, I've tried your suggestion and now I'm facing a mismatch error. I've made the *after* section into a range. This is the last cell of the column which is being searched. I really can't see what is going wrong with this


    Sub StoreNumbers()
    
    
    Dim FoundCell As Range
    Dim lastCell As Range
    Dim FirstAddr As String
    Dim a As Range 'List of stores
    Dim b As Range 'List of store numbers
    Dim c As Range 'Conversation headers
    
    
    
    For Each a In Worksheets("Reference").Range("L2:L1000")
    
    Set c = Sheets("Recieved").Range("C2:C1000")
    Set lastCell = Sheets("Recieved").Range("C1000")
    Set FoundCell = c.Find(what:=a, after:=lastCell, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, _
    searchdirection:=xlNext)
    
    If Not FoundCell Is Nothing Then
    FirstAddr = FoundCell.Address
    Do
    Set FoundCell = c.FindNext(FoundCell)
    
    FoundCell.Offset(0, 12).Value = a
    
    Loop Until FoundCell.Address = FirstAddr
    End If
    
    Next
    
    
    
    End Sub
    Any ideas?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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