Solved

create new file from multiple files

Posted on 2011-02-11
15
251 Views
Last Modified: 2012-05-11
Hi,

I want to fill in an Excel file with headers have been setup from several excel files in the same folder.
Assume those excel have the same format.  Is it possibe?

Excel to be created:
USERID  Payment1  Payment2  Payment3


Excel to be read from:
USERID        123  
Payment1    $100
Payment2    $200
Payment3    $300


output file
Excel to be created:
USERID  Payment1  Payment2  Payment3
123        $100          $200          $300

(one file per userID)

thx
0
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 10

Expert Comment

by:cdebel
ID: 34873928
You could simply assign this VBA code to a button...

Just make sure to set your path first

Private Sub btnStartImport_Click()
    Dim sPath As String
    Dim aFile
    Dim oWB As Workbook
    Dim nRow As Long
   
    ' Setup the path where your files are placed
    sPath = "C:\test"
    aFile = Dir(sPath & "\*.xlsx")
    nRow = 4
    While aFile <> ""
        Set oWB = Application.Workbooks.Open(sPath & "\" & aFile)
        Sheet1.Cells(nRow, 1) = oWB.Sheets(1).Cells(1, 2)
        Sheet1.Cells(nRow, 2) = oWB.Sheets(1).Cells(2, 2)
        Sheet1.Cells(nRow, 3) = oWB.Sheets(1).Cells(3, 2)
        Sheet1.Cells(nRow, 4) = oWB.Sheets(1).Cells(4, 2)
        oWB.Close False
        aFile = Dir
        nRow = nRow + 1
    Wend
End Sub
0
 
LVL 10

Accepted Solution

by:
cdebel earned 250 total points
ID: 34873963
Here's the code in an Excel file.

I assume that you use Excel 2010.  If you use a previous version, you could simply change this line to set it to .xls instead of .xlsx:
    aFile = Dir(sPath & "\*.xlsx")


You could also use text files instead of Excel files by using OpenText instead of open there.  In this case you would have to set the delimiter to whatever you want (a space for example)
       Set oWB = Application.Workbooks.Open(sPath & "\" & aFile)


Import.xlsm
0
 

Author Comment

by:mcrmg
ID: 34874054
thank you
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:mcrmg
ID: 34875146
I am sorry that the q is closed, I will reopen this.

I just found out that, there is a section under the example I posted above.

Excel to be read from:
USERID        123  
Payment1    $100
Payment2    $200
Payment3    $300




Expense   $200
Food         $100
Cable        $600
Travel        $300
Toll            $200

We have about 5 different items, each time the order is different and it does not show 5 items all the time, so for this section, is there a way to look for the "item", once it is found, it will grab the value?

output file
Excel to be created:
USERID  Payment1  Payment2  Payment3   Expense  Food  Cable  Travel  Toll
123        $100          $200          $300          $200        $100  $600   $300   $200

If Toll is not there, the section will be
USERID        123  
Payment1    $100
Payment2    $200
Payment3    $300


Expense   $200
Food         $100
Cable        $600
Travel        $300

output will be
USERID  Payment1  Payment2  Payment3   Expense  Food  Cable  Travel  Toll
123        $100          $200          $300          $200        $100  $600   $300  


thanks



 
0
 

Author Comment

by:mcrmg
ID: 34875158
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34875188
no need to reopen, i'll help you anyway, just give me a couple of min and i come back with your answer
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34875314
Here's your new version.   It's a bit more complex since i got to find the column where i need to put the data.  I also enhanced it a bit because every time it opened a file, the screen kept flicking.  So i've created an invisible Excel App to open those workbooks.

 Import.xlsm
0
 

Author Comment

by:mcrmg
ID: 34875407
Thank you very much for the quick reply.  Is there a way to do the "stupid way", look for the item name.

I forgot to mention that the out put file a a template, the headers can not be changed.  So, "Toll" in the input file could be name "Bridge Fee" in the output file....sorry about that
0
 

Author Comment

by:mcrmg
ID: 34875432
Let's say from A20 to A25, I need to look for "Toll"  If it exists, it will then get the value two cells to the right and put it to the "BridgeFee" in the output file.

thanks
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34875491
can you post the sample files please?

This new version look for the item name already.  As item name, it take the 1st column of the files that you need to merge.   Then it try to find that item in the header column of the workbook where you put all those data.  Once it's found, it know in which column where it need to place the data.

Now, if you don't have BridgeFee as column in the file where you merge everything, it won't know where to put that data.  I could simply code an exception for this specific column, but i would prefer to see some samples files with real column names to save some time.

I'll be back in 1h
0
 

Author Comment

by:mcrmg
ID: 34875984
0
 

Author Comment

by:mcrmg
ID: 34876019
I just upload a file, tab1 is input file, tab2 is output file.   (they should be separated.  Should have a lot of input files in the same folder)

As you can see, output headers does not matched up with the ones in input file.  So, I need to do the "stupid" way by pin point the cell to the upper part.  (upper part will never changed)


The item name in the low part in the input file never changed as well, but they dont show up all the time.  thx
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34890770
mcrmg: i've not found any new thread for this question.  The easiest way to accomplish what you want would be to put a row with headers that you find in your sources files that you want to merge.

ex:
Row 2 contain headers from your sources (you can hide it if you don't want to see it)
Row 3 contain headers that you wish to see in your output file.

But as Vee suggested, it would be better to open a new case if you have further questions.
0
 

Author Comment

by:mcrmg
ID: 34890850
ok, thank you
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question