Solved

create new file from multiple files

Posted on 2011-02-11
15
250 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

713 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