+ Reply to Thread
Results 1 to 13 of 13

My loop is missing last iteration

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

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,879

    Re: My loop is missing last iteration

    Your code wasn't looping through the files in the folder

    Try

    Sub test()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim mRng As Range
        Dim cRng As Range
        Dim mCell As Range
        Dim cCell As Range
    
        Dim filename As String
        Dim folderPath As String
        
        Set Mwb = ActiveWorkbook
        Set mws = Worksheets(1)
        
        lr = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
        
        Set mRng = mws.Range("h1:h" & lr)
        
        folderPath = "C:\DATA\"
        
        file = Dir(folderPath)
        Application.ScreenUpdating = False
        
        While (file <> "")
        
            Set wb = Workbooks.Open(folderPath & file)
            Set ws = wb.Worksheets(1)
            
            lr = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
            
            Set cRng = ws.Range("H1:H" & lr)
                
                For Each mCell In mRng
                        For Each cCell In cRng
                            If cCell = mCell Then mCell.Offset(0, 1) = cCell.Offset(0, 1)
                        Next cCell
                 Next mCell
            
            Workbooks(file).Close SaveChanges:=False
            
            file = Dir
        
        Wend
      
      mws.Range("h1") = mRng
      
      Application.ScreenUpdating = True
    
    End Sub
    Somewhat confused by which column the numbers are in : your code suggests "I", your description "G" and an image "T"!!!

    The comparison loop could be replace by MATCH function.

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

    Re: My loop is missing last iteration

    Quote Originally Posted by JohnTopley View Post
    Your code wasn't looping through the files in the folder

    Try

    Sub test()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim mRng As Range
        Dim cRng As Range
        Dim mCell As Range
        Dim cCell As Range
    
        Dim filename As String
        Dim folderPath As String
        
        Set Mwb = ActiveWorkbook
        Set mws = Worksheets(1)
        
        lr = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
        
        Set mRng = mws.Range("h1:h" & lr)
        
        folderPath = "C:\DATA\"
        
        file = Dir(folderPath)
        Application.ScreenUpdating = False
        
        While (file <> "")
        
            Set wb = Workbooks.Open(folderPath & file)
            Set ws = wb.Worksheets(1)
            
            lr = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
            
            Set cRng = ws.Range("H1:H" & lr)
                
                For Each mCell In mRng
                        For Each cCell In cRng
                            If cCell = mCell Then mCell.Offset(0, 1) = cCell.Offset(0, 1)
                        Next cCell
                 Next mCell
            
            Workbooks(file).Close SaveChanges:=False
            
            file = Dir
        
        Wend
      
      mws.Range("h1") = mRng
      
      Application.ScreenUpdating = True
    
    End Sub
    Somewhat confused by which column the numbers are in : your code suggests "I", your description "G" and an image "T"!!!

    The comparison loop could be replace by MATCH function.
    The loop is skipping some values strange?

    Here is a link to my DATA folder
    http://www.megafileupload.com/kmyK/DATA.zip

    also for my xlsm file

    http://www.megafileupload.com/kmyM/Loop.xlsm

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

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: My loop is missing last iteration

    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

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

    Re: My loop is missing last iteration

    Your example is missing value did you notice that?

    Thank you in advance.

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: My loop is missing last iteration

    Just try to remove that line
    mws.Range("h1") = mRng
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

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

    Re: My loop is missing last iteration

    Quote Originally Posted by YasserKhalil View Post
    Just try to remove that line
    mws.Range("h1") = mRng
    You have made it totally correct @YasserKhalil

    there is another issue.

    The issue is which order the loop read the files. cause the macro write over current values.

    Why I thought your version of the macro is was missing values but actually my bad.




    MFU7Quv.png


    The macro ads First Test1.xlsx the values 30 and 40 then it adds test2.xlsx all cells even if they are empty.
    So I need to create an criteria to the loop that asks" If Same Row Column "I" is empty skip the cell if not add

    Do you see what I mean Master?

    Thank you in advance.

  8. #8
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,879

    Re: My loop is missing last iteration

    Try

    Sub test()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim mRng As Range
        Dim cRng As Range
        Dim mCell As Range
        Dim cCell As Range
    
        Dim filename As String
        Dim folderPath As String
        
        Set Mwb = ActiveWorkbook
        Set mws = Worksheets(1)
        
        lr = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
        
        Set mRng = mws.Range("h1:h" & lr)
        
        folderPath = "C:\Data\"
        
        file = Dir(folderPath)
        Application.ScreenUpdating = False
        
        While (file <> "")
        
            Set wb = Workbooks.Open(folderPath & file)
            Set ws = wb.Worksheets(1)
            
            lr = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
            
            Set cRng = ws.Range("H1:H" & lr)
                
                For Each Cell In cRng
                If IsNumeric(Cell.Offset(0, 1)) Then
                    res = Application.Match(Cell, mRng, 0)
                    If Not IsError(res) Then
                        mws.Cells(res, "I") = Cell.Offset(0, 1)
                    End If
                End If
                
                Next Cell
            
            Workbooks(file).Close SaveChanges:=False
            
            file = Dir
        
        Wend
      
      
      
      Application.ScreenUpdating = True
    
    End Sub

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

    Re: My loop is missing last iteration

    Still get same result :S sir.

    3cquRGI.png

  10. #10
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,879

    Re: My loop is missing last iteration

    Results when I run with your data:

    I01 10
    I02
    I03 30
    I04 40
    I05
    I06 60

    Check your data for leading/trailing blanks.

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

    Re: My loop is missing last iteration

    Quote Originally Posted by JohnTopley View Post
    Results when I run with your data:

    I01 10
    I02
    I03 30
    I04 40
    I05
    I06 60

    Check your data for leading/trailing blanks.

    that strange see attached file

    http://www.megafileupload.com/gav3/test.zip

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

    Re: My loop is missing last iteration

    Quote Originally Posted by JohnTopley View Post
    Results when I run with your data:

    I01 10
    I02
    I03 30
    I04 40
    I05
    I06 60

    Check your data for leading/trailing blanks.

    You were correct!
    Awesome thank you very much.

    I am actually rewriting the function so I could understand it.

    Could you please help me to understand this part of your code.

    How does this work also could give another example of how to make just this snippet?


    For Each Cell In cRng
                If IsNumeric(Cell.Offset(0, 1)) Then
                    res = Application.Match(Cell, mRng, 0)
                    If Not IsError(res) Then
                        mws.Cells(res, "I") = Cell.Offset(0, 1)
                    End If
                End If
    
    Next Cell

    Thank you so much anyway.

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: My loop is missing last iteration

    @elmnas

    Even though this is solved, please note your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ 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] 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