Link to home
Start Free TrialLog in
Avatar of mcrmg
mcrmg

asked on

create new file from multiple files

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
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

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
ASKER CERTIFIED SOLUTION
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg
mcrmg

ASKER

thank you
Avatar of mcrmg

ASKER

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



 
no need to reopen, i'll help you anyway, just give me a couple of min and i come back with your answer
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
Avatar of mcrmg

ASKER

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
Avatar of mcrmg

ASKER

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
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
Avatar of mcrmg

ASKER

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
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.
Avatar of mcrmg

ASKER

ok, thank you