+ Reply to Thread
Results 1 to 7 of 7

Extract data from multiple workbooks to master

Hybrid View

  1. #1
    Forum Contributor unley's Avatar
    Join Date
    11-27-2008
    Location
    South Australia
    MS-Off Ver
    MS Office 2007
    Posts
    253

    Extract data from multiple workbooks to master

    I'd like to thank JBeaucaire for the codes he created that I found in this forum but this codes copy the whole data on a sheet

    Sub Consolidate()
    'Author:     JBeaucaire'
    'Date:       9/15/2009     (2007 compatible)'
    'Summary:    Open all Excel files in a specific folder and merge data'
    '            into one master sheet (stacked)'
    '            Moves imported files into another folder'
    Dim fName As String, fPath As String, fPathDone As String, OldDir As String
    Dim LR As Long, NR As Long
    Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet
    
    'Setup
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        
        Set wbkNew = ThisWorkbook
        Set ws = wbkNew.Sheets("Start") 'sheet report is built into...edit to match
        
        If MsgBox("Import new data to this report?", vbYesNo) = vbNo Then Exit Sub
        
        If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
            ws.Range("A2:A" & Rows.Count).EntireRow.ClearContents
            NR = 2
        Else
            NR = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
        End If
    
    'Path and filename
        OldDir = CurDir             'memorizes the user's current working path
        fPath = "G:\KEEP\Bowden\Imported\"
        fPathDone = "G:\KEEP\Bowden\Imported\Master\"   'optional
        ChDir fPath
        fName = Dir("Book*.xl*")      'filtering key, change to suit
    
    'Import a sheet from found file
        Do While Len(fName) > 0
            'Open file
                Set wbkOld = Workbooks.Open(fName)
            'Find last row and copy data
                Sheets(1).Activate
                LR = Range("A" & Rows.Count).End(xlUp).Row   'find the bottom row of data...change to a different column if "A" isn't reliable for spotting this value
                Range("A2:A" & LR).EntireRow.Copy _
                    ws.Range("A" & NR)
            'close file
                wbkOld.Close False
            'Next row
                NR = Range("A" & Rows.Count).End(xlUp).Row + 1
            'move file to "imported" folder
                Name fPath & fName As fPathDone & fName         'optional
            'ready next filename
                fName = Dir
        Loop
    
    'Cleanup
        ActiveSheet.Columns.AutoFit
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    
    'restores user's original working path
        ChDir OldDir
    End Sub
    I'm try to find a way to copy range A2:B17 instead of copy the whole data on a sheet.
    I tried to change this code to range A2:B17
     LR = Range("A" & Rows.Count).End(xlUp).Row   'find the bottom row of data...change to a different column if "A" isn't reliable for spotting this value
                Range("A2:A" & LR).EntireRow.Copy _
                    ws.Range("A" & NR)
    but with no luck
    Last edited by unley; 09-23-2010 at 11:53 PM.
    I'm using MS Office 2013

  2. #2
    Forum Contributor pr4t3ek's Avatar
    Join Date
    10-13-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2003 & 2007, 2010
    Posts
    483

    Re: Extract data from multiple workbooks to master

    try this. It may not work, I cannot test it...

    Sub Consolidate()
    'Author:     JBeaucaire'
    'Date:       9/15/2009     (2007 compatible)'
    'Summary:    Open all Excel files in a specific folder and merge data'
    '            into one master sheet (stacked)'
    '            Moves imported files into another folder'
    Dim fName As String, fPath As String, fPathDone As String, OldDir As String
    Dim LR As Long, NR As Long
    Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet
    
    'Setup
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        
        Set wbkNew = ThisWorkbook
        Set ws = wbkNew.Sheets("Start") 'sheet report is built into...edit to match
        
        If MsgBox("Import new data to this report?", vbYesNo) = vbNo Then Exit Sub
        
        If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
            ws.Range("A2:A" & Rows.count).EntireRow.ClearContents
            NR = 2
        Else
            NR = ws.Range("A" & Rows.count).End(xlUp).Row + 1
        End If
    
    'Path and filename
        OldDir = CurDir             'memorizes the user's current working path
        fPath = "G:\KEEP\Bowden\Imported\"
        fPathDone = "G:\KEEP\Bowden\Imported\Master\"   'optional
        ChDir fPath
        fName = Dir("Book*.xl*")      'filtering key, change to suit
    
    'Import a sheet from found file
        Do While Len(fName) > 0
            'Open file
                Set wbkOld = Workbooks.Open(fName)
            'Find last row and copy data
                Sheets(1).Activate
                'LR = Range("A" & Rows.count).End(xlUp).Row   'find the bottom row of data...change to a different column if "A" isn't reliable for spotting this value
                'Range("A2:A" & LR).EntireRow.Copy ws.Range("A" & NR)
                Range("A2:B17").Copy ws.Range("A" & NR)
            'close file
                wbkOld.Close False
            'Next row
                'NR = Range("A" & Rows.count).End(xlUp).Row + 1
                NR = Range("A" & Cells.count).End(xlUp).Cells + 1
            'move file to "imported" folder
                Name fPath & fName As fPathDone & fName         'optional
            'ready next filename
                fName = Dir
        Loop
    
    'Cleanup
        ActiveSheet.Columns.AutoFit
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    
    'restores user's original working path
        ChDir OldDir
    End Sub
    --
    Regards
    PD

    ----- Don't Forget -----

    1. Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks

    2. Thank those who have helped you by Clicking the scales above each post.

    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  3. #3
    Forum Contributor unley's Avatar
    Join Date
    11-27-2008
    Location
    South Australia
    MS-Off Ver
    MS Office 2007
    Posts
    253

    Re: Extract data from multiple workbooks to master

    Thank you pr4t3ek for replying

    Unfortunetley there's an error on this code on 'Next row

            NR = Range("A" & Cells.Count).End(xlUp).Cells + 1
    It has successfully pasted from the first workbook but then there's a runtime error on the above code before it'll be able to do something with the second workbook (there's only 2 workbook to copy from)
    Last edited by unley; 09-23-2010 at 08:58 PM.

  4. #4
    Forum Contributor pr4t3ek's Avatar
    Join Date
    10-13-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2003 & 2007, 2010
    Posts
    483

    Re: Extract data from multiple workbooks to master

    just comment that section and see what happens:

    Sub Consolidate()
    'Author:     JBeaucaire'
    'Date:       9/15/2009     (2007 compatible)'
    'Summary:    Open all Excel files in a specific folder and merge data'
    '            into one master sheet (stacked)'
    '            Moves imported files into another folder'
    Dim fName As String, fPath As String, fPathDone As String, OldDir As String
    Dim LR As Long, NR As Long
    Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet
    
    'Setup
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        
        Set wbkNew = ThisWorkbook
        Set ws = wbkNew.Sheets("Start") 'sheet report is built into...edit to match
        
        If MsgBox("Import new data to this report?", vbYesNo) = vbNo Then Exit Sub
        
        If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
            ws.Range("A2:A" & Rows.count).EntireRow.ClearContents
            NR = 2
        Else
            NR = ws.Range("A" & Rows.count).End(xlUp).Row + 1
        End If
    
    'Path and filename
        OldDir = CurDir             'memorizes the user's current working path
        fPath = "G:\KEEP\Bowden\Imported\"
        fPathDone = "G:\KEEP\Bowden\Imported\Master\"   'optional
        ChDir fPath
        fName = Dir("Book*.xl*")      'filtering key, change to suit
    
    'Import a sheet from found file
        Do While Len(fName) > 0
            'Open file
                Set wbkOld = Workbooks.Open(fName)
            'Find last row and copy data
                Sheets(1).Activate
                'LR = Range("A" & Rows.count).End(xlUp).Row   'find the bottom row of data...change to a different column if "A" isn't reliable for spotting this value
                'Range("A2:A" & LR).EntireRow.Copy ws.Range("A" & NR)
                Range("A2:B17").Copy ws.Range("A" & NR)
            'close file
                wbkOld.Close False
            'Next row
                'NR = Range("A" & Rows.count).End(xlUp).Row + 1
                'NR = Range("A" & Cells.count).End(xlUp).Cells + 1
            'move file to "imported" folder
                Name fPath & fName As fPathDone & fName         'optional
            'ready next filename
                fName = Dir
        Loop
    
    'Cleanup
        ActiveSheet.Columns.AutoFit
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    
    'restores user's original working path
        ChDir OldDir
    End Sub

  5. #5
    Forum Contributor unley's Avatar
    Join Date
    11-27-2008
    Location
    South Australia
    MS-Off Ver
    MS Office 2007
    Posts
    253

    Re: Extract data from multiple workbooks to master

    Your last code did'nt work but I've found a way round to it.

    Instead of comment both section
                'NR = Range("A" & Rows.count).End(xlUp).Row + 1
                'NR = Range("A" & Cells.count).End(xlUp).Cells + 1
    I took the comment off the first section and works fine
                NR = Range("A" & Rows.count).End(xlUp).Row + 1
                'NR = Range("A" & Cells.count).End(xlUp).Cells + 1
    So thank you very much for your time pr4t3ek

  6. #6
    Forum Contributor pr4t3ek's Avatar
    Join Date
    10-13-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2003 & 2007, 2010
    Posts
    483

    Re: Extract data from multiple workbooks to master

    no probs!

    i didn't think it would have worked... o well, top work! cheers!

  7. #7
    Registered User
    Join Date
    09-24-2010
    Location
    mundra
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Extract data from multiple workbooks to master

    =IF(VLOOKUP(B20,C13:C19,1,FALSE),"Duplicate","OK")

    i get #NA instead of "OK" when "Duplicate" is not applicable

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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