Good Morning
I have two csv files containing data from two of our sites.
The structure of the csv's is the same, field headers and such.
I need to merge the two together to create just one csv.
They are:
C:\qryData-Lee.csv
C:\qryData-Lon.csv
Can I do this with a command button?
Merge csv files
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England
Merge csv files
Cheers ...
Dave.
Dave.
-
- Administrator
- Posts: 78631
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Merge csv files
Here is some code using the "old" BASIC file manipulation commands:
Code: Select all
Sub MergeCSV()
Dim strFolder As String
Dim strFile1 As String
Dim strFile2 As String
Dim strFile3 As String
Dim f1 As Long
Dim f2 As Long
Dim f3 As Long
Dim strLine As String
' Path and filenames
strFolder = "C:\"
strFile1 = "qryData-Lee.csv"
strFile2 = "qryData-Lon.csv"
strFile3 = "qryData-Combined.csv"
' Output file
f3 = FreeFile
Open strFolder & strFile3 For Output As #f3
' Open first file
f1 = FreeFile
Open strFolder & strFile1 For Input As #f1
' Read line by line and write to output file
Do While Not EOF(f1)
Line Input #f1, strLine
Print #f3, strLine
Loop
Close #f1
' Open second file
f2 = FreeFile
Open strFolder & strFile2 For Input As #f2
' Discard the first line (column headers)
Line Input #f2, strLine
' Read line by line and write to output file
Do While Not EOF(f2)
Line Input #f2, strLine
Print #f3, strLine
Loop
Close #f2
Close #f3
End Sub
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 1728
- Joined: 25 Jan 2010, 08:34
- Location: Stoke on Trent - Staffordshire - England