Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

create new file from multiple files

Posted on 2011-02-11
15
Medium Priority
?
253 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:Christian de Bellefeuille
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:
Christian de Bellefeuille earned 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
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:Christian de Bellefeuille
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:Christian de Bellefeuille
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:Christian de Bellefeuille
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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,…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

715 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