473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

can multiple csv files be merged into xls file dependant on column selection?

I receive a number of different csv files daily. The data i need is contained in different columns in each (and the format cannot be changed before i receive the files). The file names are different and change on a daily basis with the suffix being yyyy/mm/dd. Can i automate the process of selecting the respective columns from each file, copying the records and then merging the records?
Attached Files
File Type: zip help.ZIP (821 Bytes, 144 views)
Jul 16 '13 #1
25 3085
zmbd
5,501 Expert Mod 4TB
Version of Office/Excel?
Do the CSV files have a "header" row?
Please tell me that they are not using the "/" in the file names...

More than likely, you'll have to import the entire document into a temporary worksheet and then parse out the information you need.


This is the basics for handling them manually (and I do mean basic :) ): Import or export text (.txt or .csv) files
Jul 16 '13 #2
ADezii
8,834 Expert 8TB
  1. As asked by zmbd, is there a Header Row in the CSV Files?
  2. Header Row for some or all of the Files?
  3. How many CSV Files are there?
  4. Are all CSV Files located in the same Folder?
  5. Are the Base Names of the CSF Files consistent?
  6. Does each CSV File represent the same number of Records in it as the others?
  7. What are the Column Specifications, namely extracting which Columns from what CSV Files?
  8. Provide some concrete Data, upload a *.csv File if possible.
  9. etc...
Jul 16 '13 #3
zmbd
5,501 Expert Mod 4TB
Actually, you need not upload the CSV file, if you will click on the [CODE/] format button and paste the header row and the first few rows of the data between the code tags. This way we can all review the data without having to download and open a file.
Jul 16 '13 #4
ADezii
8,834 Expert 8TB
@zmbd
The reason that I ask for an actual File Upload is that there may be Control Codes embedded in the File depending on the Originating Source. This could possibly affect the importing/extracting of such data. There is nothing like having the Original Data on hand to test as opposed to a graphical representation of it. Perhaps I am just getting a little too old! (LOL).
Jul 16 '13 #5
zmbd
5,501 Expert Mod 4TB
Hmm, hadn't considered that there might be embedded control codes.
I blame that on a lack of diversity given that all of the CSV files I've worked with are pretty plan old boring alphanumeric with occasional literal strings in quotes kind of stuff. With that in mind, what had envisioned for OP was a direct copy and paste from the file itself so that anything that is "printable" would be available in a subset of data instead of potentially a huge number of records.
Jul 16 '13 #6
jimatqsi
1,271 Expert 1GB
You'll want to use Dir() to scan the folder containing the Excel files. As you find them, link to them with
Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel97, "TableNameToUseForLink", SpreadSheetFileName, True, , True
to link to each spreadsheet. The "acSpreadsheetTypeExcel97" parameter will change depending on the version of Excel involved.

Once you have a link to the spreadsheet you can open it on a recordset. Then you can loop through the rows and columns to figure out what you've got - but this requires that the spreadsheets follow some kind of regular pattern. The spreadsheets don't have to all have the same columns, but you have to be able to recognize the columns for what they are.

I hope that helps.

Jim
Jul 16 '13 #7
zmbd
5,501 Expert Mod 4TB
jimatqsi:
- OP is talking about CSV files which I am aware that newer versions of Excel can open directly; however, to do requires human interaction to correlate the incoming data as these are still not native Excel files.
- Also, OP is wanting to import the files into Excel, not Access; therefore, the DoCmd.TransferSpreadsheet command is not available. In fact, none of the DoCmd.(*) commands are available for use in Excel.


mugwy2264:
Yet, jimatqsi does have a one partial solution with the DIR() in that as ADezii points out if there is a common suffix/root to the files we can scan for these and build the import function. From there it will depend upon the method as to how we can extract the columns you require and discard the remaining chaff.
Jul 16 '13 #8
ADezii
8,834 Expert 8TB
My initial idea is to:
  1. Open the Office File Dialog, with the Filter set to *.csv and MultiSelect = True.
  2. Select all the necessary *.csv Files and Import them into Access Tables.
  3. Incorporate some form of Validation making sure you have the required number of Files.
  4. Extract the required Data from these Tables based on Name and Column, then Append the results to a Results Table.
  5. Export the Results Table to Excel if so desired.
Jul 17 '13 #9
1.As asked by zmbd, is there a Header Row in the CSV Files? Yes, see attached eg
2.Header Row for some or all of the Files? All.
I’ve attached 102 and 015
3.How many CSV Files are there? 6, differently named
4.Are all CSV Files located in the same Folder?
They will be
5. Are the Base Names of the CSF (csv?)Files consistent? Other than date suffix, yes
6.Does each CSV File represent the same number of Records in it as the others?
No varies between <8 and >20000 and there are more columns in some than others
7.What are the Column Specifications, namely extracting which Columns from what CSV Files? From those prefixed “102” columns A,B,C,D,E,G, & J from “015” columns A,B,C,F,G,I, & R. The header titles remain constant in name and position.
Jul 17 '13 #10
jimatqsi
1,271 Expert 1GB
Maybe the best way to handle it is with DoCmd.TransferText. Transfer each text file into a new table (because column layout is unpredictable) then examine the columns to figure out how to process it before deleting the new table. Here's a real good discussion on stackoverflow ... http://stackoverflow.com/questions/3...sv-file-in-vba
Jul 17 '13 #11
zmbd
5,501 Expert Mod 4TB
mugwy2264
Are you taking this into Access or Excel?
For some reason I thought you started out with taking CSV texts into Excel workbook in your original post.
Jul 17 '13 #12
zmbd
5,501 Expert Mod 4TB
Ops, should have added this to that last:

7.What are the Column Specifications, namely extracting which Columns from what CSV Files? From those prefixed “102” columns A,B,C,D,E,G, & J from “015” columns A,B,C,F,G,I, & R. The header titles remain constant in name and position
I can't open your files here at work, one reason I asked for a cut and paste of the the inforamtion, the attachment is OK as it was requested by ADEzii.

If the header 102.A == 015.A (header1), etc...
then let's change mind sets a tad...
If you are in Access, when you import the CSV, the header names will become field names in the table. Therefor, instead of refering to them as column A,B,C... lets start thinking in terms of what you need, the information under the headers named: "Header1", "Header2", "Header3","Header4","Header5","Header6", and "Header7".
(now I'm also hoping that the titles for 102.G == 015.I and that 102.J == 015.R)

So in Access, the thought is to import the CSV into a "New" table for each file. Your header names become field names. Run an update query against this new table on the field names ("header*") that you are after to merge this data into your working table. Finally delete your import table.
Jul 17 '13 #13
I'm trying to bring the .csv files in to excel.
Jul 17 '13 #14
zmbd
5,501 Expert Mod 4TB
That is what I thought.
Then NONE of the "docmd" VBA commands are available to you nor is the approach in Post#13.
Jul 17 '13 #15
ADezii
8,834 Expert 8TB
My concept is to perform ALL the work in Access, and then Export the Results to Excel. If this approach is acceptable, here is the 1st piece of the puzzle:
  1. Open the Microsoft Office File Dialog Box filtered for *.csv Files only and the MultiSelect Property set to True.
  2. The Default Folder to search in will be the Project Directory, namely CurrentProject.Path.
  3. Select as many *.csv Files as you like, then click the Import Command Button.
  4. All *.csv Files will now be Imported into your Access DB with Table Names reflective of the Base File Names only, as in:
    Expand|Select|Wrap|Line Numbers
    1. Inv 015 EDW FULL 20130716
    2. Inv 102 PAX FULL 20130716
    3.  
  5. At this point it should be a relatively simple matter to extract your Data, append it to a Results Table, then Export to Excel.
  6. If you are interested in this approach let me know, and if not there are other alternatives which you can take.
  7. I have posted the Code that will actually automate the process of Importing all the *.csv Files for your reference.
    Expand|Select|Wrap|Line Numbers
    1. 'First, set a Reference to the Microsoft Office XX.X Object Library
    2. Dim varItem As Variant
    3. Dim strButtonCaption As String
    4. Dim strDialogTitle As String
    5. Dim strHyperlinkFile As String
    6.  
    7. 'Define your own Captions if necessary
    8. strButtonCaption = "Import"
    9. strDialogTitle = "Select CSV File(s) to Import"
    10.  
    11. With Application.FileDialog(msoFileDialogFilePicker)
    12.   With .Filters
    13.     .Clear
    14.       .Add "CSV Files", "*.csv"     'Allow only *.csv File types
    15.   End With
    16.   'The Show Method returns True if 1 or more files are selected
    17.     .AllowMultiSelect = True
    18.     .ButtonName = strButtonCaption
    19.     .InitialFileName = CurrentProject.Path
    20.     .InitialView = msoFileDialogViewDetails     'Detailed View
    21.     .Title = strDialogTitle
    22.   If .Show Then
    23.     For Each varItem In .SelectedItems
    24.       DoCmd.TransferText acImportDelim, , Replace(Mid$(varItem, InStrRev(varItem, "\") + 1), ".csv", ""), _
    25.                          varItem, True
    26.     Next varItem
    27.   End If
    28. End With
Jul 17 '13 #16
Hope this helps, thanks for assistance so far:
File names I refer to as 102 are like this : Inv 102 EDW FULL 20130715.csv
File names I refer to as 015 are like this: Inv 015 EDW FULL 20130715.csv
And are stored, once extracted in a file location ending in ….\Daily csv reports. The 20130715 suffix changes daily such that today’s files would end 20130717.

From those prefixed “102”
column A,(title is site)B,(title is Part No),C,(title is Part Description),D,(title is Serial No),E,(title is Lot/Batch No),G,(title is Condition Code Description), & J,(title is Qty Onhand)

from “015” column A,(title is Site),B,(title is Part No),C,(title is Part Description),F,(title is Lot/Batch No),G,(title is Serial No),I,(title is Condition Code Description), & R,(title is Qty Onhand
This is the full list from 015:
Site Part No Part Description Lot/Batch No Serial No Condition Code Condition Code Description EC W/D/R Warehouse Bay Row Tier Bin Rotable Part Pool Id Qty Onhand Qty Reserved Qty In Transit U/M Freeze Flag Last Activity Date Last Count Date Location Type Receipt Date Availability Control Id Supplier No Avg Unit Transit Cost Count Variance Expiration Date Ownership Owner Owner Name Part Acquisition Value Total Acquisition Value Acquisition Currency
Jul 17 '13 #17
ADezii
The ACCESS route would be fine, thanks for help so far
Jul 17 '13 #18
ADezii
8,834 Expert 8TB
No problem, I will not write all the Code for you since this is not a code writing service. If you execute the beginning Code that I have provided, you will have all your *.csv Files Imported into your DB as Tables named after the Base Names. Do you know how to proceed after this?
Jul 17 '13 #19
zmbd
5,501 Expert Mod 4TB
Take what ADezii has for you and then follow thru on the concept in Post#13.
The export back to excel can be done via the trasnsferspreadsheet method as mentioned in post#7.
Be aware though that the trasnsferspreadsheet uses a "stored" query (one that you can see in the Access Object Navigation Pane) and will not take a SQL string as one would expect in VBA. There are several threads about this here on Bytes.
Jul 17 '13 #20
zmbd
5,501 Expert Mod 4TB
Hey,
My mind said, why involve Access... I like Access, I think that OP would be well advised to move eveything to Access; however, back to that little voice in my head, what about treating the CSV as a database via the ADODB connection.

AH-HA-I-Said...
And I have a nice little late bind method that pulls the data in and, for now, prints the values to the debug screen.
I just need to tweek the monster a bit and records will go to the worksheet.

Here's just enough to wet the gums and teeth:

Expand|Select|Wrap|Line Numbers
  1. Sub OpenTheDataFileForReading(ByVal zstrinfolder As String, ByVal zstrinfile As String)
  2. Dim zobjcn As Object
  3. Dim zrs As Object
  4. Dim zstrcn As String
  5. Dim zstrdatadirectory As String
  6. Dim zstrdatasource As String
  7. Dim zstrSQL As String
  8. '
  9. 'get the file
  10. zstrdatadirectory = zstrinfolder
  11. '
  12. zstrdatasource = "[" & zstrinfile & "]"
  13. '
  14. 'build the connection string
  15. zstrcn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  16.     "Data Source=" & zstrdatadirectory & _
  17.     "; Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";"
  18. '
  19. 'open the connection
  20. Set zobjcn = CreateObject("ADODB.Connection")
  21. If zobjcn Is Nothing Then
  22.     MsgBox "Failed to create the object, sorry"
  23.     Exit Sub
  24. Else
  25. (...)The rest omitted for now
  26. While I tweek the code(...)
Jul 18 '13 #21
zmbd
5,501 Expert Mod 4TB
Done - ofcourse, this is in v2010 as a macro enabled workbook (*.xlsm); however, I am fairly certain that it will work in v2007.

It can have more bells and whisles added to it; however works quite nicely.

To keep some things simple, I've used the FileDialogObject with the multiple select set to true. The only issue I've had with that is that no matter how one selects the files, they are pulled from the selecteditems collection in numericalpha order (so all of the 015 prefix would import before all of the 102 etc). Can either tag these in an adjacent column, within the rows, or finally create a new worksheet for each imported file.

:)
Jul 18 '13 #22
ADezii
8,834 Expert 8TB
@zmbd:
Another approach would be to grab each Line of the Files using Low Level I/O and immediately Append the Field's Data to a pre-existing Results Table. The Fields to extract would vary from File to File, but in the case of Inv 102*.csv:
Expand|Select|Wrap|Line Numbers
  1. Dim strPathToFile As String
  2. Dim strLine As String
  3. Dim varLine As Variant
  4. Dim MyDB As DAO.Database
  5. Dim rstResults As DAO.Recordset
  6. Dim intLineNum As Integer
  7.  
  8. Set MyDB = CurrentDb
  9. Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
  10.  
  11. strPathToFile = "C:\_Transfer SCV Files\Inv 102 PAX FULL 20130716.csv"
  12.  
  13. Open strPathToFile For Input As #1
  14.  
  15. 'For File Inv 102, extract Columns A,B,C,D,E,G, and J
  16. Do While Not EOF(1)                     'Loop until end of file.
  17.   Line Input #1, strLine                'Read line into variable.
  18.     intLineNum = intLineNum + 1
  19.       varLine = Split(strLine, ",")     'Parse the Line using ','
  20.         With rstResults                 'Results Table
  21.           If intLineNum <> 1 Then       '1st Line is Field Names, ignore
  22.             .AddNew                     'Fields to extract for Inv 102*.csv
  23.               ![Site] = varLine(0)
  24.               ![Part No] = varLine(1)
  25.               ![Part Description] = varLine(2)
  26.               ![Serial No] = varLine(3)
  27.               ![Lot/Batch No] = varLine(4)
  28.               ![Condition Code Description] = varLine(6)
  29.               ![Qty Onhand] = varLine(9)
  30.           .Update
  31.         End If
  32.       End With
  33. Loop
  34.  
  35. Close #1                                'Close file.
  36. rstResults.Close                        'Cleanup
  37. Set rstResults = Nothing
Jul 19 '13 #23
zmbd
5,501 Expert Mod 4TB
ADezii,
You never fail to impress!! I'll be adding that code to the tool box for future reference.

The reason I went with the ADODB is that the field names are the same between both 102 and 015 prefix; thus, the select query only needs to be written in one format to import from both prefixed files.
Jul 19 '13 #24
ADezii
8,834 Expert 8TB
The reason I went with the ADODB is that the field names are the same between both 102 and 015 prefix; thus, the select query only needs to be written in one format to import from both prefixed files.
Makes perfect sense to me, and a great approach! Either way, the OP had several options to take, but he/she still has a job ahead! (LOL).
Jul 19 '13 #25
zmbd
5,501 Expert Mod 4TB
that's an understatement.
Jul 19 '13 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: x. zhang | last post by:
Hi Guys, We know that we can use <input type=file ...> to upload one file per time to the server. My question is if there are some way to upload multiple files per time to the server. (Of...
1
by: wcc | last post by:
Hello group, Can I use EasyDialogs to select multiple files? I saw the function AskFileForOpen but which does not have a flag allowing selection of multiple files. Did I miss something? ...
1
by: Primo | last post by:
Hello, I am building a data management application with the following processes: Process 1 is a Windows service which uses FileSystemWatcher to monitor a directory. Process 2 opens a file...
1
by: makthar | last post by:
Hi Everyone: I want to make a page for the users to be able to upload multiple files to the server. Presently I use a html file field but it allows selection of a single file only. Does any...
3
by: siddhi | last post by:
Hi, I am very very new to vb6. I am trying to browse and read multiple microsoft word files. i am able to do for one file as follow, but not multiple files. Please I need help ASAP. Thanks Private...
5
by: cybersangeeth | last post by:
Hi, I need to read 1KB each time from multiple files in a folder and pass it to a byte array in a struct to be sent through a socket. I'm a C++ newbie. I managed to read 1KB each time from one...
2
by: rengask | last post by:
I got the code to find and replace within an open text file. ------------------ Private Sub cmdFile_Click() Dim strTemp As String txtFile = "" dlg.FileName = "*.*" dlg.ShowOpen ...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.