Combining multiple Microsoft Excel Comma Separated Values

Stacy Rueda

Board Regular
Joined
Jun 23, 2016
Messages
87
Hi everyone!

Please help me with combining multiple Microsoft Excel Comma Separated Values, I want to combine my 40 csv files in one excel without opening and copy paste them one by one, and everyday our production machine producing this csv file.After creating a macro tool, I hope this tool will help me to combine into my master data if possible it will be formatted in pivot table. Thank you so much. can someone help me, if you have code already and have a big heart to share it with me.

Any assistance regarding this matter will be much appreciated.
 
Why not hit both at the same time by changing this line:
Code:
If (Cells(i, "F").Value) = "The power of machine was turned on." Then
to this:
Code:
If Left(Cells(i, "F"),31) = "The power of machine was turned" Then
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Upvote 0
Noted above sir, it's my fault. It's just I think nobody will make a reply with this thread because you are the one replying with my questions. And because this is urgent, I thought by creating a new thread will help me as soon as possible..But thank you for reminding me about the rules of our forum. and for answering my questions. May God bless you sir for your generous heart of giving reliable answers..
 
Upvote 0
Hi @Joe4, can I add another question related to the code above. I have post here that I have two rows to be deleted in all my CSV files right? which is like below:

Loc. Cell A1 - V1.02, Cell B1 - MultiLanguage
Loc. Cell A2 - Event Date, Cell B2 - Event Name and so on...

But why is that my first (no.1) CSV file did not delete it's
Loc. Cell A1 - V1.02, Cell B1 - MultiLanguage. I hope you can still help me with this..But Loc. Cell A2 is OK, i need that for my main header of all my imported CSV files. Thank you..
 
Upvote 0
It sounded like you had two header rows, so it allowed two for the first file, and then ignored the first two rows on all the other files.

One easy solution would be to just delete the first row after the imports are created (so add a line to delete the first row at the end of your code).
 
Upvote 0
That final code would look like this:
Code:
Sub ImportCSV()

    Dim strSourcePath As String
    Dim strDestPath As String
    Dim strFile As String
    Dim strData As String
    Dim x As Variant
    Dim Cnt As Long
    Dim r As Long
    Dim c As Long
    
    Application.ScreenUpdating = False
    
    'Change the path to the source folder accordingly
    strSourcePath = "D:\Error Log\Error Log_Y"
    
    If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"
    
    'Change the path to the destination folder accordingly
    strDestPath = "D:\Error Log\Error Log_Y"
    
    If Right(strDestPath, 1) <> "\" Then strDestPath = strDestPath & "\"
    
    strFile = Dir(strSourcePath & "*.csv")
    
    Do While Len(strFile) > 0
        Cnt = Cnt + 1
        If Cnt = 1 Then
            r = 1
        Else
            r = Cells(Rows.Count, "A").End(xlUp).Row + 1
        End If
        Open strSourcePath & strFile For Input As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
            If Cnt > 1 Then
                Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , strData
                Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , strData
            End If
            Do Until EOF(1)
                Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , strData
                x = Split(strData, ",")
                For c = 0 To UBound(x)
                    Cells(r, c + 1).Value = Trim(x(c))
                Next c
                r = r + 1
            Loop
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Name strSourcePath & strFile As strDestPath & strFile
        strFile = Dir
    Loop
    
    Application.ScreenUpdating = True
    
[B][COLOR=#ff0000]    Rows(1).Delete[/COLOR][/B]
    
    If Cnt = 0 Then _
        MsgBox "No CSV files were found...", vbExclamation
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top