Results 1 to 13 of 13

My loop is missing last iteration

Threaded View

  1. #1
    Forum Contributor
    Join Date
    02-23-2015
    Location
    Sweden
    MS-Off Ver
    2013
    Posts
    109

    My loop is missing last iteration

    Hello everyone

    I got a master file containing numbers in column H.


    See pictures
    VLfTF4L.png


    also I have got lots seperate files in a folder "C:\data\"
    each of these files contains numbers in column H

    File 1:
    LSkuVP2.png

    File2:
    TuQ3ERP.png


    Now I need to loop through all cells in column H in the master file, compare each cell in column H(master file) to the each of the each number in the column H in the seperate files.
    If the number is the same copy the value in the seperate file from the column G same row; paste it in the column G in the master file in the same row as the first number but column G.

    My problem is my loop is missing the last iteration I dont know why.

    See my code below:

    Sub test() 
        Dim wb As Workbook 
        Set Mwb = ActiveWorkbook 
        
        Dim folderPath As String 
        folderPath = "C:\DATA\" 
        Dim filename As String 
        filename = Dir(folderPath & "*.xlsx") 
    
        For i = 1 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 
            MioNr = Cells(i, "H").Value      
             
            Set wb = Workbooks.Open(folderPath & filename) 
            wb.Activate 
             
            For x = 1 To ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 
                CIonr = Cells(x, "H").Value 
                 
                If CIonr = MioNr Then 
                    Cells(x, "I").Copy 
                    Mwb.Activate 
                    Cells(i, "I").Activate 
                    ActiveSheet.Paste 
                    wb.Close 
                End If 
            
            Next x  
        Next i 
    
    End Sub
    Could someone help me?

    Thank you in advance
    Last edited by elmnas; 10-22-2016 at 07:45 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Go to next loop iteration if current loop has error
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-03-2016, 09:54 PM
  2. VBA - How to continue inner loop while going to next iteration of outer loop?
    By alviniac in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-14-2015, 11:21 PM
  3. jump to xth iteration of for next loop
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-25-2015, 12:07 PM
  4. [SOLVED] Last Iteration of Loop?
    By saintplay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2015, 08:19 PM
  5. [SOLVED] Loop fails on 2nd iteration
    By capson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-14-2014, 01:38 PM
  6. Continue next iteration of For loop
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-22-2013, 02:09 AM
  7. Advancing to the next iteration of a loop?
    By Maury Markowitz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2005, 01:06 PM

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