+ Reply to Thread
Results 1 to 34 of 34

Combine Data from different workbooks

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Combine Data from different workbooks

    Is it possible too do this if they are on the same computer. Lets say I want all the data on page3 in one workbook, data on page2 in another and have it show up in another workbook. If this can be done can it delete cell entry that match?

    Thanks
    Last edited by zplugger; 04-21-2009 at 04:52 PM.

  2. #2
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    I hope this is a better example, this data is just test stuff.If this can be done, what do I need too read up on. Not sure where to start, could I use Query for this?. I made a couple of test books as examples. The first two books have some matching data, will I be able too delete the matches and combine the two books on book3.
    If you have some input on this "Thank You"
    Attached Files Attached Files

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    Will the workbooks be open?
    Will they all be in one Directory?
    Will the data have a header row?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Thanks royUK for your time looking at this, no the books will be closed,yes they are in same Dir,yes the header will stay. Not even sure this can be done.

  5. #5
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Bump, has anyone ever did this, or should I look for another way?

  6. #6
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Bump,Bump Any ideas on this,where and what should I read.

  7. #7
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Maybe this will be better,If I only have 2 books. Here is a example, I can make this work if it can be done. Not sure how too go about it, Marco? Query?

    Larry.
    Attached Files Attached Files

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    Why is this post marked solved?

  9. #9
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Roy I changed it to try and make my example easy. Been doing a lot of reading and I think merging would be better. Here is my other workbooks.I will keep both workbooks in a dir called C:\test.




    Thanks
    Attached Files Attached Files

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    Ypu've marked the thread solved that would suggest that you have received an answer.

    Place this code in your master workbook. This code goes in a standard module - it will prompt the user to pick the folder containing the files - keep a separate folder for these files.

    
    Option Explicit
    
    ' API declarations
    Declare Function SHGetPathFromIDList Lib "shell32.dll" _
                                         Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal _
                                                                                           pszpath As String) As Long
    
    Declare Function SHBrowseForFolder Lib "shell32.dll" _
                                       Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) _
                                       As Long
    
    Public Type BrowseInfo
        hOwner     As Long
        pIDLRoot   As Long
        pszDisplayName As String
        lpszTitle  As String
        ulFlags    As Long
        lpfn       As Long
        lParam     As Long
        iImage     As Long
    End Type
    
    Function GetDirectory(Optional msg) As String
        On Error Resume Next
        Dim bInfo  As BrowseInfo
        Dim sPath  As String
        Dim r As Long, x As Long, pos As Integer
    
        'Root folder = Desktop
        bInfo.pIDLRoot = 0&
    
        'Title in the dialog
        If IsMissing(msg) Then
            bInfo.lpszTitle = "Please select the folder containing the Excel files to copy."
        Else
            bInfo.lpszTitle = msg
        End If
    
        'Type of directory to return
        bInfo.ulFlags = &H1
    
        'Display the dialog
        x = SHBrowseForFolder(bInfo)
    
        'Parse the result
        sPath = Space$(512)
        r = SHGetPathFromIDList(ByVal x, ByVal sPath)
        If r Then
            pos = InStr(sPath, Chr$(0))
            GetDirectory = Left(sPath, pos - 1)
        Else
            GetDirectory = ""
        End If
    End Function
    Place this code in another module, it will do the actual merge - using the above function to get the folder
    Sub Get_Data_From_All()
        Dim wbSource As Workbook
        Dim wbThis As Workbook
        Dim rToCopy As Range
        Dim uRng   As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
    
            On Error GoTo exithandler
    
            Set wbThis = ThisWorkbook
            'clear the range except  headers
            Set uRng = wbThis.Worksheets(1).UsedRange
            If uRng.Cells.Count <= 1 Then
                'no data in master sheet
                bHeaders = False
            Else: uRng.Offset(1, 0).Resize(uRng.Rows.Count - 1 _
                                           , uRng.Columns.Count).ClearContents
                bHeaders = True
            End If
    
            With .FileSearch
                .NewSearch
                'Get directory containing files
                .LookIn = GetDirectory
                .FileType = msoFileTypeExcelWorkbooks
    
                If .Execute > 0 Then    'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count    ' Loop through all.
                        'Open Workbook x and Set a Workbook  variable to it
                        Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        Set rToCopy = wbSource.ActiveSheet.UsedRange
                        With wbThis.Worksheets(1)
                            Set rNextCl = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
                            If bHeaders Then
                                'headers exist so don't copy
                                rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                            rToCopy.Columns.Count).Copy rNextCl
                                'no headers so copy
                                'place headers in Row 2
                            Else: rToCopy.Copy .Cells(2, 1)
                                bHeaders = True
                            End If
                        End With
                        wbSource.Close False     'close source workbook
                    Next lCount
                Else: MsgBox "No workbooks found"
                End If
            End With
            MsgBox "Done", vbInformation, "Import Data"
            Exit Sub
    exithandler:
            MsgBox "aborted"
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    End Sub

  11. #11
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Thanks roy, I'm a little slow on this stuff so please bare with me. Do I place both codes in the Journal1 book? I have both books in a directory called invoice. Are these code for the two journals I posted last.
    Thanks

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    I used your original workbooks - master, test1 & test2.

    The code goes in the workbook that you are importing to. The workbooks containing the data to import in any other directory - I would set up a specific folder to hold them.

    can I ask why the data is in different workbooks?
    Last edited by royUK; 04-20-2009 at 08:35 AM.

  13. #13
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    the journal1 & 2 are closer too what I'm doing. I have too workbooks with the journals on sheet 4 and I hope too make the code work on them. I did what you said and must be doing something wrong. Here is my folder, when I run this will I get all entrys on the journal too one sheet?.
    Attached Files Attached Files

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    You hadn't copied the code correctly. The attached workbook contains the code set to copy data from sheet4 of all workbooks in the specified folder
    Attached Files Attached Files

  15. #15
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    roy Please bare with me and don't quit on me. Thank you very much on helping me, just not sure whats wrong. I have put these 3 workbooks in the same forder. When I run the master it asks what folder and I go and pick it. But when I run the marco in the master nothing happens? I know it must be something I'm doing, you taking a lot of time for this and its looks real good, exactly what I want too do.
    Attached Files Attached Files

  16. #16
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    OK roy, making progress, I'm able too pull the data from the two books. What I only can get one book at a time, if I do the other book it deletes the other data. If I can I want all data too stay in the master.

  17. #17
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    hello roy
    I not good at explaining on what I want, let me do it in steps
    Get the data from both workbooks and save it too a master journal
    When I input data in one of the workbooks I want too go to the master journal and update. The journal will store all entrys. Can this be done, sorry if I confused you. You have been a great help.

  18. #18
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    would it be better if I just post my two workbooks?

  19. #19
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    Move the master out of that folder.

    If you want them all in one folder I will have to amend the code.

  20. #20
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    I don't understand why it is necessary. Why have two journals, what is the data stored from?

    It looks to me as if you are storing invoice data, this should all be done in the same workbook

  21. #21
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    Last edited by royUK; 04-20-2009 at 01:08 PM.

  22. #22
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Thanks roy, here are the two workbooks I use. Both books have a journal on page 4, what is the best way too combine the data in the journals. The books are a mess now,been working on them, I will clean up. The invoice number works for both books, I just need a source too combine all data in journal. The journal helps be keep track of payment. With the setup I have is this possible too do? Maybe the journals could merge somehow?
    Attached Files Attached Files

  23. #23
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    Just had a look at your latest workbooks. I think you ar making this far more complicated than necessary. Take a look at my suggestion in the link in my last post.

  24. #24
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    I'm getting lost again,roy I use both workbooks because my jobs are different. I guess I could do a hole new workbooks with both types of daily jobs but that would be a lot of work. For now I just looking for a easy way too combine my journals. Now I have too open both books and search the journals for what I need. I look at the other examples and not sure how it will work for me, so what I want do do would be real hard? I can stay with my old way if it going too be hard too do. You been great working on this for me.
    Thank You

  25. #25
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    I really don't see the need for different workbooks, but why not have a separate journal book & save to that instead?

    I see what you mean about the code needing tidying up(LOL). Try using this for your ComboBox1
    
    Private Sub ComboBox1_Change()  'add entry to invoice
        Dim NextCl As Range
        With Sheet1
            Set NextCl = .Cells(55, 2).End(xlUp).Offset(1, 0)
            If IsEmpty(NextCl) Then
                NextCl.Value = Me.ComboBox1.Text
            Else: MsgBox "Invoice is full. Add extra lines", vbCritical, "Input error"
                Exit Sub
            End If
        End With
    End Sub

  26. #26
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    roy a separate journal book will work if I can save both books too it. Is they a way too save to a journal book instead of page4 in the books. I do not have too have that page if I can have a book. I would name it Jornal Book and put it in the same folder as my workbooks. What type of code would that take?

  27. #27
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    I see where you are using VLOOKUP in the code, but you don't need to wipe these formulas in the sheet. Just clear the main body of the invoice, as I have in the example I pointed you to.

    I've created a named range for the invoice body that the button will clear
    The address book sheet contains two Dynamic Named Ranges (AddressBook - used in VLOOKUP, and names used in the Data validation)

    I've made some changes to the userform, you need to remove all the code that is not actually used.

    Let me know if you want to write the details straight to a master journal
    Attached Files Attached Files

  28. #28
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Let me know if you want to write the details straight to a master journal , yes if this can be done. Can you write too a workbooks that is closed? Will I be able too do the same with my 2 day form. All I need is the info that is on page 4

  29. #29
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    one other thing, could I merge my oneday into my twoday for one book?

  30. #30
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    Quote Originally Posted by zplugger View Post
    one other thing, could I merge my oneday into my twoday for one book?
    Try this updated book, I have changed the code & design. The invoice body is now populated using Data validation. You can add to the product list & it will be inluded automatically in the drop down.
    Attached Files Attached Files

  31. #31
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Combine Data from different workbooks

    Thanks royUK I will input it into my form now, its nice too have help from a expert.

  32. #32
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Combine Data from different workbooks

    The lists are Data validation, see

    http://www.excel-it.com/data_validation.htm

    You should always consider non-VBA approaches first. Even the invoice number could be generated using a formula.

+ 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