+ Reply to Thread
Results 1 to 5 of 5

Loop to copy the same rows from multiple files to a master file

Hybrid View

  1. #1
    Registered User
    Join Date
    07-25-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Loop to copy the same rows from multiple files to a master file

    Hi! I am really quite new to macros and am trying to get up to speed

    I am trying to create a macro which copies an individual row from over a hundred separate excel files (all formatted identically) and pastes them into a separate master file with them all listed consecutively. I assume I need a for loop but cant work out how to get it to loop through separate files. I then have trouble coding to get the pasting to move down a row on the master sheet.

    Any help would be much appreciated!!!

    Thanks

    Alex

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,925

    Re: Loop to copy the same rows from multiple files to a master file

    Post a simple example of your workbook.
    When you are in Advanced reply, click on the paperclip to open the upload window.

    View Pic
    Last edited by protonLeah; 07-25-2012 at 10:37 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    07-25-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Loop to copy the same rows from multiple files to a master file

    Hi- thank you for replying.

    Each workbook contains daily summary information for 16 different water monitoring sites (WM-01 to WM-16). I need to create a separate workbook which contains every day of information for just 1 site (e.g. WM-14).

    For example, in the simplified attached workbook, I need to copy rows 16-18 (site WM-14) and paste this information into another workbook. This process needs to be repeated for all the files in the folder (about 100 and all formatted identically) in order to obtain all of the information from all days of monitoring for just site WM-14 in one 'master' workbook.

    I therefore need a code to loop through each file, copying rows 16-18 of each and pasting them consecutively one after another in a master workbook.

    All help, greatly, greatly appreciated!
    Thank you!
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,925

    Re: Loop to copy the same rows from multiple files to a master file

    try this one:
    Option Explicit
    
    Sub Copy2Tab()
        Dim SiteIDList  As Range, _
            SiteID      As Range, _
            LastID      As Range, _
            Source      As Worksheet, _
            Dest        As Worksheet, _
            NextRow     As Long
            
        Application.ScreenUpdating = False
    
        Set Source = Sheets("Sheet1")
        With Source
            Set SiteIDList = .Range("A3:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
        End With
        
        For Each SiteID In SiteIDList
            'check for "(" in id and ignore
            
            If InStr(SiteID.Value, "(") > 0 Then
                SiteID.Value = Mid(SiteID, 1, InStr(SiteID.Value, "(") - 1)
            End If
            Err.Clear
            On Error Resume Next
            Set Dest = Sheets(SiteID.Value)
            If Err.Number > 0 Then
                Err.Clear
                Sheets.Add after:=Sheets(Sheets.Count)
                ActiveSheet.Name = SiteID.Value
                Set Dest = Sheets(SiteID.Value)
                Source.Range("A1:R2").Copy Dest.Range("A1")
            End If
            
            'find the next row in dest sheet
            NextRow = Dest.Cells(Rows.Count, "A").End(xlUp).Row + 1
            NextRow = IIf(NextRow < 3, 3, NextRow + 1)
            Source.Range(SiteID.Address).EntireRow.Copy Dest.Cells(NextRow, "A")
        Next SiteID
        Source.Select
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    07-25-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Loop to copy the same rows from multiple files to a master file

    Thank you so much for this. It works well- I could never have written that myself!

    The only issue is that I would like to be able to get the code to loop through every Excel file in the folder (i.e. get the source file to change). However, when the code runs through these files, the information from each file needs to be copied to a common (master) sheet for each site (e.g. WM-01) (i.e. the sheet created during the first loop), rather than opening a new set of sheets for each file.

    For example, row 4 from both the attached files ('SWM_example1', 'SWM_example2') needs to be copied to the same 'WM-02' sheet (which was created by your fantastic code, shown in your example, 'SWM_example(bvj)'), rather than creating a new 'WM-02' sheet everytime the code loops.

    In my mind you are already a genius, but if you can help me with tweaking the code to do this, you will be STELLAR.

    Many thanks
    Attached Files Attached Files

+ 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