+ Reply to Thread
Results 1 to 18 of 18

Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

Hybrid View

emilx3 Combining many .CSV files... 02-24-2016, 10:53 PM
emilx3 Re: Combining many .CSV files... 02-25-2016, 06:36 AM
alansidman Re: Combining many .CSV files... 02-25-2016, 06:40 AM
Doc.AElstein Re: Combining many .CSV files... 02-25-2016, 01:20 PM
emilx3 Re: Combining many .CSV files... 02-25-2016, 06:24 PM
Doc.AElstein Re: Combining many .CSV files... 02-25-2016, 08:09 PM
Doc.AElstein Re: Combining many .CSV files... 02-26-2016, 02:36 PM
emilx3 Re: Combining many .CSV files... 03-10-2016, 07:56 PM
jindon Re: Combining many .CSV files... 03-10-2016, 10:00 PM
jindon Re: Combining many .CSV files... 03-11-2016, 05:53 AM
emilx3 Re: Combining many .CSV files... 03-11-2016, 06:54 AM
jindon Re: Combining many .CSV files... 03-11-2016, 07:07 AM
emilx3 Re: Combining many .CSV files... 03-11-2016, 07:21 AM
jindon Re: Combining many .CSV files... 03-11-2016, 08:10 AM
emilx3 Re: Combining many .CSV files... 03-11-2016, 03:28 PM
Doc.AElstein Re: Combining many .CSV files... 03-11-2016, 04:32 PM
  1. #1
    Registered User
    Join Date
    02-24-2016
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    8

    Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Dear all

    I have a bunch of data in many different .CSV files that I am trying to combine into a single .xls file using a macro. Each .CSV file have a unique name representing the data source and consists of 3 columns: A) timestamp, B) logged/recorded value, and C) Unit (i.e. Volts)

    Now to the part that gives me trouble: I would like to create a single .XLS file that contains all the data from the different .CSV files added in consecutive columns. And with the .CSV filename added to an inserted first/top row.

    I have found and modified two different macros that will either combine the data into a single .XLS file (sheet) or inset a header for each dataset in the first row of a . XLS sheet, but I have no idea how to make both macros work together as one.

    Here is my first macro that adds filenames above each dataset:

    Option Explicit
    
    Sub ImportCSVsWithReference()
    
    Dim wbCSV   As Workbook
    Dim wsMstr  As Worksheet:   Set wsMstr = ThisWorkbook.Sheets("Ark1")
    Dim fPath   As String:      fPath = "Y:\AnyPathWithCSVfiles\" 
    Dim fCSV    As String
    Dim NextCol As Long
    
    If MsgBox("Delete exsisting data?", _
        vbYesNo, "Delete exsisting data before import?") = vbYes Then
            wsMstr.UsedRange.Clear
            NextCol = 1
    Else
            NextCol = wsMstr.Cells(3, Columns.Count).End(xlToLeft).Column + 1
    End If
    Application.ScreenUpdating = False  'Speeds up the macro
    fCSV = Dir(fPath & "*.csv")         'start the CSV file listing
    
        Do While Len(fCSV) > 0
          'open a CSV file
            Set wbCSV = Workbooks.Open(fPath & fCSV)
          'Adding a first row containing the filename
            Range("A1") = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".csv") - 1)
          'Copy source and closes source
            ActiveSheet.UsedRange.Copy wsMstr.Cells(1, NextCol)
            wbCSV.Close False
          'ready next CSV
            fCSV = Dir
            NextCol = wsMstr.Cells(3, Columns.Count).End(xlToLeft).Column + 3 'Adding columns for the data
        Loop
     Application.ScreenUpdating = True
    End Sub
    My next little macro that combines the .CSV files into a single .XLS file looks like this:

    Sub import()
        Dim Str1 As String
        Dim i As Integer
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = True
            .Filters.Clear
            .Filters.Add "Excel Files (*.xls)", "*.xls"
            .Filters.Add "All Files (*.*)", "*.*"
            .Show
            If .SelectedItems.Count > 0 Then
                Worksheets(1).Activate
                For i = 1 To .SelectedItems.Count
                Str1 = "TEXT;" & .SelectedItems.Item(i)
                    With ActiveSheet.QueryTables.Add(Connection:=Str1, Destination:=Cells(1, 3 * i))
                        .TextFileSemicolonDelimiter = True
                        .TextFileDecimalSeparator = "."
                        .TextFileThousandsSeparator = ","
                        .Refresh BackgroundQuery:=False
                    End With
                Next
            End If
        End With
    End Sub

    I sorry if my syntax if offensive or cluttered, but I hope it makes sense?

    Best regards, Emil
    Attached Files Attached Files
    Last edited by alansidman; 02-25-2016 at 06:40 AM. Reason: code tags added

  2. #2
    Registered User
    Join Date
    02-24-2016
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    8

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Hi again

    Maybe I overcomplicated the question?

    Thanks to Jerry Beaucaire I have found the following code that works like a charm except that it does not handle ";" delimiters. How can Jerrys code be modified to do that?:

    Option Explicit
    
    Sub ImportCSVsWithReference()
    'Author:    Jerry Beaucaire
    'Date:      10/16/2010
    'Summary:   Import all CSV files from a folder into a single sheet
    '           adding a field in column A listing the CSV filenames
    
    Dim wbCSV   As Workbook
    Dim wsMstr  As Worksheet:   Set wsMstr = ThisWorkbook.Sheets("MasterCSV")
    Dim fPath   As String:      fPath = "C:\2010\Import\"    'path to CSV files, include the final \
    Dim fCSV    As String
    
    If MsgBox("Clear the existing MasterCSV sheet before importing?", vbYesNo, "Clear?") _
        = vbYes Then wsMstr.UsedRange.Clear
    
    Application.ScreenUpdating = False  'speed up macro
    
    fCSV = Dir(fPath & "*.csv")         'start the CSV file listing
    
        Do While Len(fCSV) > 0
          'open a CSV file
            Set wbCSV = Workbooks.Open(fPath & fCSV)
          'insert col A and add CSV name
            Columns(1).Insert xlShiftToRight
            Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
          'copy date into master sheet and close source file
            ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
            wbCSV.Close False
          'ready next CSV
            fCSV = Dir
        Loop
     
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    24,992

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Hi emilx3
    I am not too familiar with the code from Jerry Beaucaire, or the others. They do come close to what you want. I cannot easily modify them for you as the .csv Files of yours seem to come up in Excel differently depending how I open them. I guess Excel guesses separators to some extent which may work for or against you depending on the weather and the day of the week. Exactly what method you then use complicates the matter.


    But i have imported .csv, ( or rather .txt ) files into an Excel Spreadsheet using a fairly simple VBA Code. I have often used the__ ; __ as delimiter in my text files.


    If you had those CSV Files as simple .txt files ( In Notebook for example ) with the data looking like this

    Start Tid;Forbrug;Enhed
    2014-01-01 00:00:00 +0100;31.6999999999971;kWh
    2014-01-01 01:00:00 +0100;31.6000000000058;kWh
    2014-01-01 02:00:00 +0100;30.8999999999942;kWh

    Then, if you run either of the codes i give below, ( you will be asked which text file to select ), after selecting a .txt file, and giving the separator that the text file uses , then you get this which will have as top left the Active cell at the start of running the code. ( here i selected cell C3 arbitrarily before running the code. )

    Using Excel 2007
    Row\Col
    C
    D
    E
    3
    Start Tid Forbrug Enhed
    4
    2014-01-01 00:00:00 +0100
    31.7
    kWh
    5
    2014-01-01 01:00:00 +0100
    31.6
    kWh
    6
    2014-01-01 02:00:00 +0100
    30.9
    kWh
    Ark1


    _............

    Your approach to using this method would be to incorporate this code within your Dir Loop, such as that in the first code you showed in Post #1
    If you wish to consider this approach and have difficulty in doing that then upload some .txt files with just a few rows in, and hand then fill in an excel File showing me exactly how you want the data in the excel file to look based on that data, and i will take a look.

    Alan

    Basic “stand-alone Codes” : ( They are the same code, one is simplified and the other has detailed explaining ‘Comments.


    'Option Explicit'Comment Out For Simplified code.
    Sub SHimpGlifiedBasicMacroTextToExcel()
    On Err GoTo TheEnd
      DtaFleNm = Application.GetOpenFilename(FileFilter:="Hallo,*.txt,")
        If DtaFleNm = False Then Exit Sub
       Sp = Application.InputBox("Enter a separator character.", Type:=2)
        If Sp = vbNullString Then Exit Sub
     StCm = ActiveCell.Column
     Rw = ActiveCell.Row
      Open DtaFleNm For Input As 3
        Do Until EOF(3)
        Line Input #3, DtaFleLn
        If Right(DtaFleLn, 1) <> Sp Then DtaFleLn = DtaFleLn & Sp
         Cm = StCm
         TxtPs = 1
         SpPs = InStr(TxtPs, DtaFleLn, Sp)
          While SpPs >= 1
           TxtDta = Mid(DtaFleLn, TxtPs, SpPs - TxtPs)
           ActiveSheet.Cells(Rw, Cm).Value = TxtDta
           TxtPs = SpPs + 1
           Cm = Cm + 1
           SpPs = InStr(TxtPs, DtaFleLn, Sp)
          Wend
         Rw = Rw + 1
        Loop
    TheEnd:
      Close 3
    End Sub
    '
    '
    '
    
    Sub BasicMacroTextFileToExcel() '     http://www.mrexcel.com/forum/excel-questions/795675-run-macro-txt-file.html#post3917591
    ' Very Simply alternative to the Spreadsheet way using Wizzard, Opening as text File in Excel  etc
    '
    'First Bit  can  be ommited if Fielnames are always known. It basically gets all the File info.
    'Note text File And Excel File must not be in the same Directory as you are being asked for it here.
    On Err GoTo TheEnd ' If anything goes wrong go to the end rather than crasching!
    '
    'First Bit: TextDataFileName and seperator input
    Dim Sp As String ' Assign an initial location where further info about the string will be added later
    Dim DtaFleNm As Variant 'The DataFilename itself is a string. But a Variant is specified as you may have a Boolean input in the Dialogue box if the user cancels
      DtaFleNm = Application.GetOpenFilename(FileFilter:="Hallo,*.txt,") 'The Application Method GetOpenFilename Displays the standard Open dialog box. The optional arguments:= writes a name,limits type of files looked for
        If DtaFleNm = False Then Exit Sub 'Stops Porgram if User Cancels (which gives the boolean False)
      Let Sp = Application.InputBox("Enter a separator character.", Type:=2) 'prompts user for separator character
        If Sp = vbNullString Then Exit Sub 'Stops Porgram if User Cancels (which gives an empty string)
    'End First Bit TextDataFileName and seperator input
    '
    '    'If you want to skip the first bit, delete it or comment it out and type in the text File path and seperator in lines like this:
    '    Dim DtaFleNm As String
    '    Let DtaFleNm = ThisWorkbook.Path & "\TextDataFile.txt"
    '    Dim Sp As String
    '    Let Sp = "|"
    '    'Note: If you do it this way you should put the Active Excel File and Data Text File in the same Directory
    '
    'Start Main Bit: Get data from text file  line by line, then for each line go through every Column. As each data bit is found it is put in the approriate place in the Excel File
    'Before you start You should select cell
    Dim Rw As Long, StCm  As Long, Cm As Long 'Whole Numbers for rows and columns where the Split data line from the Text file goes.  Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster.
    Let StCm = ActiveCell.Column 'where you want to start data to come in. Column is then reset back to left in loop
    Let Rw = ActiveCell.Row 'Row increases downwards so the initial and loop count can be same variable
    Dim DtaFleLn As String 'Using our method we bring in a line from the text File at a time
    Dim TxtPs As Byte 'text character position in text file looking form left
    Dim SpPs As Byte 'position of seperater lookinf from left
    Dim TxtDta As String 'String used temporarily for each Text data value
      Open DtaFleNm For Input As 3 ' Sort of opens up a data highway and gives it route number (here 3) to distinguisch it from any others currently open. (If you do not know how many Highways are already open and have a number, then use instead of a number someting like #HgWyNm here, and HgWyNm everyhwere where the number is. This gives it the next available number (Remember to Dim it at the start also.))
        Do Until EOF(3) 'Do until going your at the end of the data highway
        Line Input #3, DtaFleLn 'Bring in a line from text file(Automatically goes to next line or next part of highway after this command. So we need mo row count for the data file.)
        If Right(DtaFleLn, 1) <> Sp Then DtaFleLn = DtaFleLn & Sp 'Glue a seperator on the end of the data file line if there is not one there
        Let Cm = StCm 'go to first column
        Let TxtPs = 1 ' Initial Data Text Position is at 1 (Ist Left poisition in text file line
        Let SpPs = InStr(TxtPs, DtaFleLn, Sp) 'Find position of next seperator
          While SpPs >= 1 'As long as a next Seperator position is found, we go througth columns looking for data
          Let TxtDta = Mid(DtaFleLn, TxtPs, SpPs - TxtPs) 'this gives the next data value
          Let ActiveSheet.Cells(Rw, Cm).NumberFormat = "@" 'This is not allways necerssary but can help avoid annoying problems with data turning into times or dates
          Let ActiveSheet.Cells(Rw, Cm).Value = TxtDta 'Put Text data in excel cell
          Let ActiveSheet.Cells(Rw, Cm).Value = Trim(ActiveSheet.Cells(Rw, Cm).Value) 'Most not necerssary but can help to clean up data if any extra spaces came in
          Let TxtPs = SpPs + 1 'Start of next data is just after next seperator
          Let Cm = Cm + 1 'Increase Excel Column number
          Let SpPs = InStr(TxtPs, DtaFleLn, Sp) 'Get the next Seperator position by starting at the current data and going right until the next seperator is found
          Wend 'go to next text data to the right in the data file Line
        Let Rw = Rw + 1 'Increase row number for Excel(The tecxt file automatically goes to the next row after reading a line
        Loop 'Go to the next Row in the Data Text file
    TheEnd:
      Close 3 'Very important to do this. Shuts highway Off. There can be strange errors if you do not do that. (It is here so that even after an error it will be done
    End Sub 'BasicMacroTextFileToExcel()
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  5. #5
    Registered User
    Join Date
    02-24-2016
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    8

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Hi Alan

    Thank you very much for your feedback. I have tried to run both your suggested macros, but didn't quite get there yet. As you also suggested I have tried to add code in the DIR loop in the code from my first post, yet I still do something wrong. It seems like no matter what I type I get a compiling error. I do wish to use that solution, but have great difficulty, so further help would be greatly appreciated

    I have attached 3 new and much smaller .CSV files and a .XLS file that is manually made to look like how I would like the macro to make it look like. Notice how the filename appears as a header above each dataset in their consecutive columns. Again your help is very much appreciated.

    Emil

    PS I forgot to mention that my first code in post #1 does import the filenames as text in first row, but the data in the following rows have not been split up into columns. In my second code from post #1 the data from each files is split up and placed into consecutive columns like I wqanted to, but then the filenames are missing in the first row...
    Attached Files Attached Files
    Last edited by emilx3; 02-25-2016 at 06:25 PM. Reason: Spelling

  6. #6
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Hi Emil,
    As I mentioned before, the problem for me is opening .csv Files. I do not know how to control the format it comes up in Excel. It comes up differently every time for me.
    _...........................................................

    However. If you Take any of your files, say the first one, and save it, but then open it with any Text editor such as Notepad then I get a file looking like this

    Start Tid;Forbrug;Enhed
    2014-01-01 00:00:00 +0100;31.6999999999971;kWh
    2014-01-01 01:00:00 +0100;31.6000000000058;kWh
    2014-01-01 02:00:00 +0100;30.8999999999942;kWh
    2014-01-01 03:00:00 +0100;30.4000000000015;kWh
    2014-01-01 04:00:00 +0100;32.5;kWh

    If i then save that as a .txt File and then run any of my codes and select that .txt File, then in Excel I get this:

    Using Excel 2007
    Start Tid Forbrug Enhed
    2014-01-01 00:00:00 +0100
    31.7
    kWh
    2014-01-01 01:00:00 +0100
    31.6
    kWh
    2014-01-01 02:00:00 +0100
    30.9
    kWh
    2014-01-01 03:00:00 +0100
    30.4
    kWh
    2014-01-01 04:00:00 +0100
    32.5
    kWh
    Ark1

    The rest to get all files as you wish to show in the excel Spreadsheet is fairly straight forward. I can do that for you, if you have difficulties, but my starting point will be to have all those Files as .txt Files

    I could probably include in my code something to convert your .csv files to .txt files if that were acceptable.
    It would also be a lot easier if all the .csv Files that you wish the program to access are typically held by you in the same Folder, and that there are no other .csv files or .txt files there. ( if there are you other files that should not be considered then I need at least to know their names ) . I do not need to know the name of the folder, but on the other hand if you know it ( the Full File Path and name like this sort of form.......
    H:\Excel0202015Jan2016\MrExcel\TextFiles
    ___....)
    Then so much the better.

    Have another go at using my code to select a .txt File, such as the one I return. Then let me know what you want me to do further.
    https://app.box.com/s/2nf4d2mtsppyr8vek0jttspz4ai057sy



    Alan
    Attached Files Attached Files
    Last edited by Doc.AElstein; 02-26-2016 at 05:44 AM.

  7. #7
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Hi Emil,

    I guess you may have this one done then in the meantime?
    If you have it is always good to share the solution.
    I just answered an almost identical Thread, jus a lot more complicated.
    So while it was fresh in my head, I did a quick mod to it to get it to do what you want.
    So i thought it would be a useful contribution to this thread.

    _...........................................................

    The code I give here is actually much too complicated with extra unnecessary bits that your application does not need. But I will leave it that form for now. The extra stuff may slow things down a bit, but as a prototype to get anyone going on a similar requirement it allows a bit of flexibility. I deliberately am using lots of different ways of doing things as a learning exercise ( for me!! ). I have not extensively tested it yet, but it takes the files you gave me and gives the results you wanted.

    So the basic idea is.....

    The requirement
    Bring any number ( within reason ) of ( Excel ) .csv files in a folder that have a form something like this.............

    Using Excel 2007
    Row\Col
    A
    B
    C
    1
    Start Tid Forbrug Enhed
    2
    2014-01-01 00:00:00 +0100
    0.01
    kWh
    3
    2014-01-01 01:00:00 +0100
    0.01
    kWh
    4
    2014-01-01 02:00:00 +0100
    0.01
    kWh
    5
    2014-01-01 03:00:00 +0100
    0.01
    kWh
    6
    2014-01-01 04:00:00 +0100
    0.01
    kWh
    Data file 2

    ________...Into a Master Sheet in a Excel, so , for example, with Three such Files you end up with This

    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    1
    Data file 1 Data file 2 Data file 3
    2
    Start Tid Forbrug Enhed Start Tid Forbrug Enhed Start Tid Forbrug Enhed
    3
    2014-01-01 00:00:00 +0100 31.6999999999971 kWh 2014-01-01 00:00:00 +0100 0.0100000000002183 kWh 2014-01-01 00:00:00 +0100 1.0 kWh
    4
    2014-01-01 01:00:00 +0100 31.6000000000058 kWh 2014-01-01 01:00:00 +0100 0.0100000000002183 kWh 2014-01-01 01:00:00 +0100 1.0 kWh
    5
    2014-01-01 02:00:00 +0100 30.8999999999942 kWh 2014-01-01 02:00:00 +0100 0.0100000000002183 kWh 2014-01-01 02:00:00 +0100 1.0 kWh
    6
    2014-01-01 03:00:00 +0100 30.4000000000015 kWh 2014-01-01 03:00:00 +0100 0.00999999999930878 kWh 2014-01-01 03:00:00 +0100 1.0 kWh
    7
    2014-01-01 04:00:00 +0100 32.5 kWh 2014-01-01 04:00:00 +0100 0.0100000000002183 kWh 2014-01-01 04:00:00 +0100 1.0 kWh
    8
    Ark1

    ( That is a screenshot BTW taken for after a run if the code, so it seems to work. )

    There can be any amount of columns and rows in the csv files ( within reason )
    You are asked for the Folder containing the csv Files
    You are asked for the separator ( In your files it was a __ ; ___ )

    The code is currently set to put data in a master Worksheet of name “Ark1” in the same file as you put the code in.



    The code is here:
    http://www.excelforum.com/showthread...t=#post4326024
    And here:
    http://www.excelforum.com/showthread...t=#post4326025

    Note it is all one code. ( I had to split it as it was too big for a single post. ) The second part should be copied directly under the first part in the same Module. That can be a normal Code Module or a Sheet Code module.

    I may come back and edit / change it a bit as i notice anything as Ii work on the other Thread i am looking at )

    Alan

    P.s.
    The basic idea wot the code is doing:

    Rem 1) Rem 2)

    Gets the folder and Separator Info from you

    Rem 4 )
    Makes a temporary copy of that Folder including all the files in it.

    Rem 5 )
    This is the “Dir” Loop, doing stuff for each of the csv files the Duh "chucks up"
    So for each of them, ( it loops while “Dir” chucks something up )

    Changes the .csv to a .txt File ( and Puts it in another Temporary folder ) ( Rem 5a) Rem 5b) )

    Rem 5c)
    This is the main doing stuff based closely on the original Codes I gave. Just has a Minor mod to put data for each inputted File at the next free column and puts the file name at the top left of each data set.

  8. #8
    Registered User
    Join Date
    02-24-2016
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    8

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Hi again

    I had to leave my problem for a few days and have now returned to find your very extensive answer, which I thank you for! have merged the code from the
    two links into one macro like you instructed, and have also tried a couple of test runs. Unfortunately my PC runs hot and crashes every time I run the macro. The first two messageboxes to appear, but then it stops working.
    I have not been able to debug the code, but I have rechecked that I did indeed copy paste the correct code.

    I have tried the following code that actually seem to solve most of my problem except that it generates an error #1004 that says something like "Excel can only konvert one column at a time"


    Sub ImportCSVsWithReference()
    
    Dim wbCSV   As Workbook
    Dim wsMstr  As Worksheet:   Set wsMstr = ThisWorkbook.Sheets("Ark1")
    Dim fPath   As String:      fPath = "Y:\AnyFilePath\" 'path to the folder woth CSV files
    Dim fCSV    As String
    Dim NextCol As Long
    
    If MsgBox("Delete exsisting data?", _
        vbYesNo, "Delete exsisting data before import?") = vbYes Then
            wsMstr.UsedRange.Clear
            NextCol = 1
    Else
            NextCol = wsMstr.Cells(3, Columns.Count).End(xlToLeft).Column + 1
    End If
    Application.ScreenUpdating = False  'Speeds up the macro
    
    fCSV = Dir(fPath & "*.csv")         'start the CSV file listing
    
        Do While Len(fCSV) > 0
          'open a CSV file
            Set wbCSV = Workbooks.Open(fPath & fCSV)
          'Adding a first row containing the filename
            Range("A1") = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".csv") - 1)
          'Copy source and closes source
            ActiveSheet.UsedRange.Copy wsMstr.Cells(1, NextCol)
            wbCSV.Close False
          'ready next CSV
            fCSV = Dir
            
            ActiveSheet.UsedRange.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
            Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
            :=Array(Array(1, 5), Array(2, 1), Array(3, 1)), DecimalSeparator:=".", _
            ThousandsSeparator:=",", TrailingMinusNumbers:=True
            
            NextCol = wsMstr.Cells(3, Columns.Count).End(xlToLeft).Column + 1 'Adding columns for the data
        Loop
     
    Application.ScreenUpdating = True
    
    End Sub
    The code I have marked in red is where the debugger says there is an error. I think I would need to modify the macro in such a way it only converts one column at a time. But that I have had no succes with so far...

    Any suggestions?

    Sincerely, Emil

  9. #9
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Hi Emil,
    I am not at my Excel computer for a while. So it is difficult to follow your new code.

    How about for now I give you the File that i have working. ( there is a lot of other junk in it , just ignore that.) The File should come up in sheet “Ark1”. ( That sheet is Empty. ) If that sheet does not come up then select that sheet.
    If you save that File into the same Folder as your 3 Original Sample Files and run the code
    Sub EmilDBlxFull()
    Then it should give you the results i showed in the screen shots.
    See how you get on. Let me know and i will try to give you more help if you need it, maybe around Saturday
    Alan
    _............................................

    File:
    https://app.box.com/s/wetlfbj6ua66a483dq7usx4261gtq3qt

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    emilx3
    See if this works as you want.
    Sub test()
        Dim myDir As String, fn As String, txt As String, x, LastC As Range
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then myDir = .SelectedItems(1) & "\"
        End With
        If myDir = "" Then Exit Sub
        fn = Dir(myDir & "*.csv")
        Do While fn <> ""
            txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(myDir & fn).ReadAll
            x = Split(txt, vbNewLine)
            If IsEmpty([a1]) Then
                Set LastC = [a1]
            Else
                Set LastC = Cells.Find("*", , , , 2, 2)(, 2)
            End If
            With LastC.EntireColumn
                .Cells(1).Value = fn
                .Cells(2).Resize(UBound(x) + 1).Value = Application.Transpose(x)
                .TextToColumns .Cells(1), xlDelimited, , , , , , , 1, ";"
            End With
            fn = Dir
        Loop
        Columns.AutoFit
    End Sub

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    No idea.
    Working for me.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    02-24-2016
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    8

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Quote Originally Posted by jindon View Post
    No idea.
    Working for me.
    Hi Jindon

    Thanks for your reply. My previous comment was ment for Doc.AElstein, but I can see that was not very clear in the tread.

    I have also tried your code, but it seems only able to take a maximum of 3 files each with a max number of rows = 7?

    The files that you had working was some simplified files that I was asked to upload earlier as the real files are several thousands of rows long. I actually have something like 20 sets of each 30 files I need to combine, but all those fiiles are way too much to upload here.

    Sincerely Emil

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Quote Originally Posted by emilx3 View Post
    I have also tried your code, but it seems only able to take a maximum of 3 files each with a max number of rows = 7?
    It should read all rows.

  14. #14
    Registered User
    Join Date
    02-24-2016
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    8

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Quote Originally Posted by jindon View Post
    It should read all rows.
    Hi Jindon

    I think we are close to a solution Could I ask you to try a couple of my original files? I will need to combine 20 files like those I have uploaded again. There might be a slight problem with a thousand separator, but I think that is easy to fix once the rest is sorted.

    Right now, when I run your script with the files I have now uploaded I get the following: billede 2.jpg

    Thank you, Emil
    Last edited by emilx3; 03-11-2016 at 07:56 AM. Reason: Picture inserted

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Th problem is the character used in the file name "ø".
    You need to delete/change it.

    Try change to
    Sub test()
        Dim myDir As String, fn As String, LastC As Range
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then myDir = .SelectedItems(1) & "\"
        End With
        If myDir = "" Then Exit Sub
        Application.DisplayAlerts = False
        Cells.Delete
        Application.DisplayAlerts = True
        fn = Dir(myDir & "*.csv")
        Do While fn <> ""
            If IsEmpty([a1]) Then
                Set LastC = [a1]
            Else
                Set LastC = Cells.Find("*", , , , 2, 2)(, 2).EntireColumn.Range("a1")
            End If
            LastC.Value = fn
            With ActiveSheet.QueryTables.Add(Connection:= _
                "TEXT;" & myDir & fn, Destination:=LastC(2))
                .TextFileSemicolonDelimiter = True
                .TextFileDecimalSeparator = "."
                .TextFileThousandsSeparator = ","
                .Refresh BackgroundQuery:=False
            End With
            fn = Dir
        Loop
    End Sub

  16. #16
    Registered User
    Join Date
    02-24-2016
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    8

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Quote Originally Posted by jindon View Post
    Th problem is the character used in the file name "ø".
    You need to delete/change it.

    Try change to
    Sub test()
        Dim myDir As String, fn As String, LastC As Range
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then myDir = .SelectedItems(1) & "\"
        End With
        If myDir = "" Then Exit Sub
        Application.DisplayAlerts = False
        Cells.Delete
        Application.DisplayAlerts = True
        fn = Dir(myDir & "*.csv")
        Do While fn <> ""
            If IsEmpty([a1]) Then
                Set LastC = [a1]
            Else
                Set LastC = Cells.Find("*", , , , 2, 2)(, 2).EntireColumn.Range("a1")
            End If
            LastC.Value = fn
            With ActiveSheet.QueryTables.Add(Connection:= _
                "TEXT;" & myDir & fn, Destination:=LastC(2))
                .TextFileSemicolonDelimiter = True
                .TextFileDecimalSeparator = "."
                .TextFileThousandsSeparator = ","
                .Refresh BackgroundQuery:=False
            End With
            fn = Dir
        Loop
    End Sub
    Hi Jindon

    Your code worked beautifully and fast Thank you very much to everybody that has helped me solve this problem. I hope I one day can contribute to this forum in such a helpfull manner as you did!

    Have a great weekend, Emil

  17. #17
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Combining many .CSV files with a ";" delimiter into a single .XLS file with header row

    Quote Originally Posted by emilx3 View Post
    .... Thank you very much to everybody that has helped me solve this problem....l
    @ Emil
    Glad you got there

    @ Jindon
    Thanks for the great and quick professional alternative. I learnt from your alternatives
    Thanks
    Alan

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Function delimiter changed from "," to ";"
    By Franco403 in forum Excel General
    Replies: 5
    Last Post: 08-31-2015, 11:51 AM
  2. [SOLVED] Replace all BLANK cells in column with header title "Balance" to "0"
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-21-2014, 09:25 AM
  3. Replies: 3
    Last Post: 11-05-2010, 03:06 PM
  4. Transfert cell values from file "A" to file "B" skipping columns in file "B".
    By Sentrosi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2009, 11:11 PM
  5. Export to CSV file using "|" as the delimiter
    By William in forum Excel General
    Replies: 1
    Last Post: 07-04-2006, 07:25 AM
  6. [SOLVED] How do I change file/open/"files of type" to default to "all file.
    By How do I change"files of type" default in forum Excel General
    Replies: 1
    Last Post: 04-19-2005, 06:06 PM
  7. Macro Help- combining "CS" files
    By Judyt in forum Excel General
    Replies: 1
    Last Post: 02-18-2005, 10:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1