+ Reply to Thread
Results 1 to 3 of 3

Copy rows from diferents books using macros

Hybrid View

  1. #1
    Registered User
    Join Date
    08-09-2011
    Location
    lima, Peru
    MS-Off Ver
    Excel 2010
    Posts
    1

    Copy rows from diferents books using macros

    Hello friends: I would like your help in the following. I have a directory with several books and each book has a different number of worksheets. Manually created a book called Maestro with a single worksheet and wanted to create a macro that opens the diferents books in directory and open eachone of the worksheets of these books by copying all the rows with data. The copy should start in row 5 of the worksheets and continue as long as there is data. Control whether data is in column A. For example if a100 (a500 or A800 can be) no data, skip to the next sheet or book. All sheets have the same structure. In the end, Maestro would have all the rows of books that are in that directory. I hope letting me explain and appreciate your time and patience. my english isnt'n good. Regards

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy rows from diferents books using macros

    'WORKBOOKS TO 1 SHEET STACKED
    Here's a macro for collecting data from all files in a specific folder.
    The parts of the code that need to be edited are colored to draw your attention.


    ===============
    Based on the code on that page including the option shown below it for cycling through multiple sheets, something like this:
    Option Explicit
    
    Sub Consolidate()
    'Author:     Jerry Beaucaire'
    'Date:       9/15/2009     (2007 compatible)  (updated 4/29/2011)
    'Summary:    Merge files in a specific folder into one master sheet (stacked)
    '            Moves imported files into another folder
    
    Dim fName As String, fPath As String, fPathDone As String
    Dim LR As Long, NR As Long
    Dim wbData As Workbook, wsMaster As Worksheet, ws As Worksheet
    
    'Setup
        Application.ScreenUpdating = False  'speed up macro execution
        Application.EnableEvents = False    'turn off other macros for now
        Application.DisplayAlerts = False   'turn off system messages for now
        
        Set wsMaster = ThisWorkbook.Sheets("Maestro")    'sheet report is built into
    
    With wsMaster
        If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
            .UsedRange.Offset(1).EntireRow.Clear
            NR = 2
        Else
            NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1    'appends data to existing data
        End If
    
    'Path and filename (edit this section to suit)
        fPath = "C:\2011\Files\"            'remember final \ in this string
        fPathDone = fPath & "Imported\"     'remember final \ in this string
        On Error Resume Next
            MkDir fPathDone                 'creates the completed folder if missing
        On Error GoTo 0
        fName = Dir(fPath & "*.xls*")        'listing of desired files, edit filter as desired
    
    'Import a sheet from found files
        Do While Len(fName) > 0
            If fName <> ThisWorkbook.Name Then              'don't reopen this file accidentally
                Set wbData = Workbooks.Open(fPath & fName)  'Open file
    
            'This is the section to customize, replace with your own action code as needed
                For Each ws In wbkData.Worksheets
                    LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row    'Find last row
                    If LR > 4 Then ws.Range("A5:A" & LR).EntireRow.Copy .Range("A" & NR)
                Next ws
    
                wbData.Close False                                'close file
                NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1  'Next row
                Name fPath & fName As fPathDone & fName           'move file to IMPORTED folder
                fName = Dir                                       'ready next filename
            End If
        Loop
    End With
    
    ErrorExit:    'Cleanup
        ActiveSheet.Columns.AutoFit
        Application.DisplayAlerts = True         'turn system alerts back on
        Application.EnableEvents = True          'turn other macros back on
        Application.ScreenUpdating = True        'refreshes the screen
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy rows from diferents books using macros

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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