+ Reply to Thread
Results 1 to 32 of 32

Pick up data from files and combine into a new file

  1. #1
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Pick up data from files and combine into a new file

    Hi,

    I have about 10 files of the same appearance. I would like to pick up certain information from each file and then combine that into a new file.

    The program should search for a certain heading, in this case for instance "Capital employed". Copy the information under "Capital employed" and paste it into the new file.

    The infomation under each heading is separated from a new heading by at least 2 blank lines.

    Enclosed is a pick up from 3 files showing the general appearance.

    Can someone help me with a suitable macro?

    Best regards,

    Jan
    Attached Files Attached Files

  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: Pick up data from files and combine into a new file

    Can you also provide the 3 files those excerpts came from for context?
    _________________
    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 Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi,

    Please, find 3 basic files from which data have been picked up.
    The number of basic files is about 10.
    The basic files have the same appearance, i.e. 10 rows with data for each heading such as "capital employed" etc.
    However, the last basic file might have fewer than 10 rows for each heading.

    Best regards,
    Jan
    Attached Files Attached Files

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

    Re: Pick up data from files and combine into a new file

    This should do it.
    Please Login or Register  to view this content.
    The parts noted in red are your controls, edit them as needed in your real workbook. The macro is intended to be stored in and run from that destination workbook.

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this demonstration !


    A similar way via selecting desired files :

    PHP Code: 
     Sub Demo1()
        
    Dim Rf As Range
        With ThisWorkbook
    :  ChDrive .Path:  ChDir .Path:  End With
        V 
    Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", , Space$(27) & "Select files :", , True)
        If 
    TypeName(V) <> "Variant()" Then Exit Sub
        Sheet1
    .UsedRange.Clear
        Application
    .ScreenUpdating False
                                
        
    For Each W In V
            With Workbooks
    .Open(W0True)
                   
    Set Rf = .Worksheets(1).UsedRange.Columns(1).Find("Capital Employed*", , xlValuesxlWhole)
                If 
    Not Rf Is Nothing Then
                  With Rf
    .CurrentRegion
                       
    .Copy Sheet1.Cells(R& + 11)
                       
    + .Rows.Count
                  End With
                End 
    If
                       .
    Close
            End With
        Next
                   Set Rf 
    Nothing
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 07-25-2015 at 09:01 PM. Reason: optimizing files filter …

  6. #6
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi,

    I have tried to make some Changes of the macro with less success.

    The file Destination.xlsm is used for the macro as well as to collect the combined data.

    From the original files File1, File2 and File3 are read the values under the first heading "Capital employed" which are combined to Sheet1 under "Destination". The name of Sheet1 is changed to "Capital employed".

    In a similar way the values under the next heading, in this case "Capital turnover" , are read and combined to Sheet2 in the destination file. The name of the sheet is changed to "Capital turnover".

    The process is repeated until all the headings have gone through. Max 10 headings. A heading stands always over the name "Company" of the original files.

    When the process has gone through all the data are collected into new files with corresponding headings.

    I do hope you can help with suitable code.

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Pick up data from files and combine into a new file

    1) there is only ONE fPATH, you tried to make 3. Put all the files to import into the same folder, list that path under fPATH.

    2) The file you uploaded didn't have those sheets, so I had to create them..... do some legwork next time.

    So here's the updated macro that has 10 target sheets in the workbook. The sheetname can't include illegal characters like / so we used dashes and then just fix it before the actual search.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this new demonstration !


    Jan, this new code creates worksheets if needed, nothing to set up in Destination workbook :

    PHP Code: 
    Sub Demo2()
        Const 
    DPATH "C:\Bibliotek\Dokument\Excel\Macron\"
          Dim Rg As Range, Ws As Worksheet
                                F$ = Dir(DPATH & "
    *.xls*"):  If F = "" Then Beep: Exit Sub
        Application.ScreenUpdating = False
                                R& = Sheet1.Rows.Count:       EW$ = "
    ¤"
        Do
            With Workbooks.Open(DPATH & F, 0, True).Worksheets(1)
                  Set Rg = .Cells(1).End(xlDown).CurrentRegion
                While Rg.Row < .Rows.Count
                    W$ = Application.Trim(Replace(Split(Split(Rg(1).Value, " 
    (")(0), ",")(0), "/", ChrW(8741)))
                    While Len(W) > 31:  P& = InStrRev(W, " "):  W = Left(W, IIf(P, P - 1, 31)):  Wend

                    If IsError(Evaluate("
    ISREF('[" & ThisWorkbook.Name & "]" & W & "'!A1)")) Then
                        For Each Ws In ThisWorkbook.Worksheets
                              If Ws.Name Like "
    Sheet*" Then Ws.Name = W: Ws.Cells.Clear: Exit For
                        Next
                        If Ws Is Nothing Then With ThisWorkbook.Worksheets: .Add(, .Item(.Count)).Name = W: End With
                        EW = EW & W & "
    ¤"
                    ElseIf InStr(EW, "
    ¤" & W & "¤") = 0 Then
                        ThisWorkbook.Worksheets(W).Cells.Clear:  EW = EW & W & "
    ¤"
                    End If

                    Rg.Copy ThisWorkbook.Worksheets(W).Cells(R, 1).End(xlUp)(3)
                    Set Rg = Rg(Rg.Rows.Count, 1).End(xlDown).CurrentRegion
                Wend
                    .Parent.Close
            End With
                   F = Dir
        Loop Until F = ""
                            SPQ = Split(EW, "
    ¤")
        For R = 1 To UBound(SPQ) - 1
            With ThisWorkbook.Worksheets(SPQ(R)):  .Rows("
    1:2").Delete:  .Columns(1).AutoFit:  End With
        Next
                Set Rg = Nothing:  Set Ws = Nothing
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 07-26-2015 at 10:44 PM. Reason: adding object variables release …

  9. #9
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi JBeaucaire,

    When I try to run the program I get the message "Subscript out of range" for the row above "if MsgBox etc".
    I have changed the basic files File1 to File3 so they should not contain any illegal characters.
    Have you any explanation?

    The program is now written using 10 fixed Headings, "Capital turnover, "Capital employed" etc. If I would like to have other Headings or variables, still 10 or less, I have to go to the program and change there. Could the program be changed so the headings are taken and read from the basic files? Of course that requires that no illegal characters are used but would create a much more flexible program. The headings can always be found on the row above "Company" in the basic files.


    Kind ragards,
    Jan
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Pick up data from files and combine into a new file



    Did you ever try my codes ?‼

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

    Re: Pick up data from files and combine into a new file

    The Destination workbook I provided for you already had the 10 sheet created for you. The one you uploaded does not, so that's why you are getting Subscript errors.

    Try Marc's macro.

  12. #12
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi Marc!

    In fact I have tried your macro.

    But i got the message on the 3rd line "Compile error. Variable not defined for F$".

    I am not so familiar with programming so I do not know how to solve it.

    Kind regards,

    Jan

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: Pick up data from files and combine into a new file



    Put in comment Option Explicit codeline or delete it to test my code …

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

    Re: Pick up data from files and combine into a new file

    Marc, please demonstrate within your code the correct declaration of all variables. Thanks.

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Pick up data from files and combine into a new file


    As written in code … For example if error occurs for F$, add F$ in Dim codeline …

    But like my variables are declared at the first appearance, my code does not need Option Explicit statement …

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

    Re: Pick up data from files and combine into a new file

    This will be my last comment, I've made my request for you regarding the declaration of variables. The benefits of Options Explicit for keeping errors from creeping into code as you develop it should be self-evident, not needing me to overly underline it again.

    So here in the teaching forum, please demonstrate proper declaration of variables that survive the existence of the very important Option Explicit.

    Thanks.

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Pick up data from files and combine into a new file

    I'm not on a PC with Excel and I will be off net for several days tomorrow …

    Jan can update Dim codeline like this :
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 07-27-2015 at 01:31 PM.

  18. #18
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi JBeaucaire,

    As I mentioned a few days ago I get the message "Subscript out of range" for the row above "if MsgBox etc".
    I have changed the basic files File1 to File3 so they should not contain any illegal characters. The destination file where also you will find the macro has 10 sheets. Do you have any explanation
    why I cannot run the macro?

    The program is now written using 10 fixed Headings, "Capital turnover, "Capital employed" etc. If I would like to have other Headings or variables, still 10 or less, I have to go to the program and change there. Could the program be changed so the headings are taken and read from the basic files? Of course that requires that no illegal characters are used but would create a much more flexible program. The headings can always be found on the row above "Company" in the basic files.

    Kind ragards,
    Jan
    Attached Files Attached Files

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

    Re: Pick up data from files and combine into a new file

    I believe that is what Marc's macro does.
    Last edited by JBeaucaire; 07-29-2015 at 05:01 PM.

  20. #20
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi Marc L,

    I have tried your macro Demo2 enclosed.

    However, even if it seems to go through, I do not get any data from the basic files File1, File2 and File3 in the directory C:\Bibliotek\Dokument\Excel\Macron\Files\

    What is the mistake?

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Pick up data from files and combine into a new file

    I just ran Marc's macro with those 3 source files in one folder. Worked great. Be sure to edit the DPATH at the top of his macro before using this.
    Attached Files Attached Files

  22. #22
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Pick up data from files and combine into a new file

    Hi,

    I have tried to use a macro "demo2" in file "Destination"
    developed by Marc L but got the the message for variable F$ on the 3 rd line "Variable not defined". I thought it was a string. But it must obviously be something else?

    Any comments?

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Pick up data from files and combine into a new file

    Jan, Marc's original code used undeclared variables, which they believe is a fine practice. We will have to agree to disagree.

    In my post #21 I installed his macro with the required edits, and it seems to operate fine. Use that version.

  24. #24
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi JBeaucaire,

    I have downloaded your macro, Demo2, but I cannot get it to function. From your No 21 contribution I can see that the final result is excellent with all 10 sheets. The question is why it is not functioning with me?

    I have copied your file, added the basic files with data, File1, File2 and File3. All files can be found in the folder Macron with the path "C:\Bibliotek\Dokument\Excel\Macron\".

    First time I run it I got the message "variable not defined" for "P&" and "SPQ". I added them to "Dim". Then the program at least accepted the changes. But it did not start running.

    Is my Office version 2003 too old?

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Pick up data from files and combine into a new file

    I opened the file from your post #24 and ran it changing only the path to my files, and it runs fine. I see nothing that should stop if from working for you.

  26. #26
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi JBeaucaire,

    This is a mystery!

    But I cannot run the macro "Demo2" in file "pick-up-data-from-files-and-combine-into-a-new-file-destination."

    You do not have any other suggestions to solve this?

    Kind regards,

    Jan

  27. #27
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi,

    The program now functions as desired.

    However, a small change might be suitable.

    Now , the headings consisting of an empty row plus the item in question plus the row "Company plus years" are repeated on the work sheet. It would be better to have it only once at the top and then delete all the others to form a big table with only the heading once. Such a big table with data and only one heading would be easier to work further with.

    How would the additional code look like?

    Kind regards,

    Jan

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

    Re: Pick up data from files and combine into a new file

    Maybe like so?
    Please Login or Register  to view this content.

  29. #29
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Pick up data from files and combine into a new file

    Quote Originally Posted by JBeaucaire View Post
    Marc's original code used undeclared variables, which they believe is a fine practice. We will have to agree to disagree
    My variables are declared ! For example, F$ = Dir(DPATH & "*.xls*")
    is the same as in VB Dim F as String = Dir(DPATH & "*.xls*") !
    It's just a BASIC heritage …

    And very easy to check : using a break point at the beginning of code or
    in step by step mode (hitting F8 key), just see the Local variables window !

    I hope this time my point of view won't be edited like in my previous post …

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

    Re: Pick up data from files and combine into a new file

    Marc, you're point is fine, but limited. The benefits of OPTION EXPLICIT are much more than just forcing you to declare your variables in a DIM. It also points to all detectable errors in the code as you go. So having to declare your variables at the top is a small price to pay for that "spell-checking" capability being there helping all the time.

    So, yes, you're method of declaring as you go is fine in theory (and in practice for experienced programmers), but that method does not play nice with OPTION EXPLICIT which (IMO) offers much more important help to learning coders.

  31. #31
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Pick up data from files and combine into a new file


    I totally agree with you as I wrote in a previous post but you edited it and have cancelled this point !
    So yes it's very usefull for beginners !

    But if I advise to just remove it to try my already tested and working code, no matter …
    As you pointed out, I got a bit of experience …

  32. #32
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Pick up data from files and combine into a new file

    Hi,

    I have got help to pick up data and combine them into new worksheets. It works fine.

    In some cases you might be interested in combining the data on each worksheet into one big table, leaving out 3 rows (blanc, the studied variable and Company + years) except for the rows at the top. I have got a suggestion of a macro but it does not function so well for me.

    If one instead starts with the worksheets created, which work properly, and limit the macro to only delete the rows except the 3 heading rows at the top how would the VBA code then be?

    Kind regards,

    Jan

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Combine Data from Closed Files within the same folder
    By lasjbp9 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-07-2013, 10:07 AM
  2. Combine data from 4 workbooks and then export filtered data to individual files?
    By womanket in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-16-2013, 12:20 PM
  3. Combine multiple Ecxel files in one master file with VB
    By jelena1290 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-10-2012, 12:36 AM
  4. Combine all files with similar names into one file
    By popvel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2012, 07:20 AM
  5. Need to combine tons of files to a master file for pivot reporting
    By betsymc in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 02-09-2012, 03:05 AM
  6. Can I combine files with some overlapping data to one file?
    By JohnJohn6025 in forum Excel General
    Replies: 0
    Last Post: 07-14-2011, 10:57 AM
  7. Replies: 4
    Last Post: 09-22-2010, 01:22 PM
  8. Replies: 1
    Last Post: 03-16-2005, 01:22 AM

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