Solved

create new file from multiple files

Posted on 2011-02-11
15
245 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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you
0
 

Author Comment

by:mcrmg
Comment Utility
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
Comment Utility
0
 
LVL 10

Expert Comment

by:cdebel
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:mcrmg
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:mcrmg
Comment Utility
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
Comment Utility
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
Comment Utility
ok, thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now