+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    08-28-2003
    Location
    Singapore
    Posts
    5

    For..Next loop and FindNext problem

    Hi, Excel community experts!

    I am trying to find a list of values from column 1 in Sheets("Find") in all the other sheets. For each value that is found in column "I" in each of the sheets, I want to copy specific cell values in that row to Sheets"Find" in column B through to column U.

    My code doesn't loop through all the sheets to search from row 9 to last used row. What is wrong with the code? Appreciate any help on my problem and thanks a bunch.

    Code:
    Option Explicit
    
    Sub FindAndCopyFromSheetsFound()
    
    Dim lastcol As Integer, lastrw As Long, lastrow As Long, lastcolumn As Integer, i As Long, j As Long
    Dim ws As Worksheet, FindSht As Worksheet, vFind, rSearch As Range
    Dim rFound As Range, rFoundCol As Integer, rFoundRow As Long, FirstAddress As String
    
    Sheets("Find").Activate
    Set FindSht = Sheets("Find")
    FindSht.Range(Cells(2, 2), Cells(1234, 25)).ClearContents
    FindSht.Range(Cells(2, 2), Cells(1234, 25)).ClearFormats
    lastcol = FindSht.Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, _
      SearchDirection:=xlPrevious).EntireColumn.Column
    lastrw = FindSht.Cells(FindSht.Rows.Count, "A").End(xlUp).Row
                    
    For Each ws In Worksheets
        lastrow = ws.Cells.Find(what:="*", after:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            
            For i = 2 To lastrw
            vFind = FindSht.Cells(i, 1)
            
            Application.ScreenUpdating = False
            'On Error Resume Next
            On Error GoTo ErrHandlr
            
            If ws.Name <> FindSht.Name Then
    
                With ws
                Set rSearch = Range("I9:I" & Cells(65536, 9).End(xlUp).Row)
    
                'Search for the first occurrence of the item
                Set rFound = rSearch.Find(what:=vFind, LookIn:=xlValues, lookat:= _
                xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
                
                    j = 2
                    If Not rFound Is Nothing Then
                
                        Do
                            FirstAddress = rFound.Address
                            rFoundCol = rFound.Column
                            rFoundRow = rFound.Row
                    
                            ws.Range("F" & rFoundRow & ":R" & rFoundRow).Copy FindSht.Cells(j, "H")
                            Debug.Print "rFound & Qty = " & rFound.Value & " Qty:" & rFound.Offset(, 3).Value
                            FindSht.Cells(j, "B").Value = ws.Name
                            ws.Range("A" & rFound.Row).End(xlUp).Resize(1, 5).Copy FindSht.Cells(j, "C")
                            ws.Range("S" & rFound.Row).End(xlUp).Copy FindSht.Cells(j, "U")
                        
                            j = j + 1
                    
                            'Search for the next cell with a matching value
                            Set rFound = rSearch.FindNext(rFound)
    
                        Loop While Not rFound Is Nothing And rFound.Address <> FirstAddress
                    End If
                End With
            End If
            
            Next i
    
    ErrHandlr: If Err.Number = 1004 Then
        Exit For
        End If
    Next ws
    
    FindSht.Range("A2").Select
    Application.ScreenUpdating = True
    
    Set rFound = Nothing
    Set rSearch = Nothing
    Set FindSht = Nothing
    
    End Sub

  2. #2
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,135

    Re: For..Next loop and FindNext problem

    Posting a workbook would help. Your code is unclear.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    08-28-2003
    Location
    Singapore
    Posts
    5

    Re: For..Next loop and FindNext problem

    Hi !

    I have attached a sample workbook, using XL 2003. All the nice-persons, could you please help to take a look what was wrong with my code?
    Attached Files Attached Files
    Last edited by William Poh Ben; 03-21-2010 at 02:05 AM. Reason: extra few lines in code

  4. #4
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: For..Next loop and FindNext problem

    There's lots of errors in your sheet, the main one is that you use With ws but don't qualify the references, in which case Excel looks at the range on the active sheet.

    You also set values for last row, but don't use them

    You try to set a range but end with .Row, this would return an error

    You declare rFoundCol as Integer so why prefix with r?

    I've amended most of these errors, but I don't think the code is giving the right results. An example showing expected results would help
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  5. #5
    Registered User
    Join Date
    08-28-2003
    Location
    Singapore
    Posts
    5

    Re: For..Next loop and FindNext problem

    Hello, Roy!

    I re-attached the sample file with an expected result sheet. I also de-commented out the unused variables and syntax.

    Example: Use this list to be pasted on column 1 in Sheets("Find") to find.

    ET-1701065
    ET-1702407
    ET-6504001408
    ET-1976999003
    ET-1976833101
    ET-1976049101
    ET-6802561808
    ET-1976332102
    ET-1976331101
    Attached Files Attached Files
    Last edited by William Poh Ben; 03-21-2010 at 09:23 AM. Reason: typo error

Thread Information

Users Browsing this Thread

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

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.2.0