Results 1 to 5 of 5

Trouble with nested for loops

Threaded View

  1. #1
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    Trouble with nested for loops

    Good afternoon,

    I'm trying to get my macro to paste row j from "master" workbook into cell A12 of sheet1 in each workbook j in E:\DIRECTORY\. The files in the directory are sorted in the same order as the rows to be copied in master. Row 1 in master goes to workbook 1 (A12 in sheet1), row 2 in master goes to workbook2 (A12 in sheet 1) etc. Below is my attempt, but it keeps looping through files until every file gets every row. I only want one row per file:

    Sub semiauto()
        
        'this sets your template workbook/worksheet
        Dim master As Workbook
    
        Set master = Workbooks("NS RA (Non-Union)")
        
      'this creates a collection of all filenames to be processed
        Dim loopFolder As String
        Dim fileNm As Variant
        Dim myFiles As New Collection
        Dim FinalRow As Long
        Dim i As Long
        
        master.Activate
        Sheets("Sheet2").Select
        FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
        '''don't forget the backslash before the final double-quote below
        loopFolder = "C:\Users\marsmanm\Desktop\PAY\MMRA1\test\"
        fileNm = Dir(loopFolder & "*.xlsm")
    
        Do While fileNm <> ""
            myFiles.Add fileNm
            fileNm = Dir
        Loop
    
        'this loops through all filenames and copies your copyWS to the beginning
        Dim wb As Workbook
        
     For i = 2 To FinalRow
        For Each fileNm In myFiles
            Set wb = Workbooks.Open(filename:=(loopFolder & fileNm))
            master.Activate
            Sheets("Sheet2").Select
            Range("A" & i, "n" & i).Copy
            wb.Activate
            Sheets("Appendix D Calc").Select
            With ActiveSheet.Range("A12")
                .PasteSpecial (xlPasteValues)
                .PasteSpecial (xlPasteFormats)
                .PasteSpecial (xlPasteColumnWidths)
            wb.Save
            wb.Close
            End With
                Next
            Next i
    End Sub
    Last edited by excel_novice2019; 01-25-2019 at 06:12 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Nested For Each loops
    By MeggieDoodles in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2015, 04:21 PM
  2. [SOLVED] Nested IF loops More than 7 Need Help
    By EricSomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2013, 02:25 PM
  3. Nested Do Loops
    By ross88guy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2010, 09:10 AM
  4. VBA - Nested loops
    By roheba in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-09-2010, 01:42 PM
  5. Having trouble with loops in a macro.
    By caliskier in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-20-2007, 12:09 PM
  6. [SOLVED] Many Nested loops
    By naterator in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-19-2006, 05:45 PM
  7. Nested with loops
    By Clair in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2006, 03:35 PM

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