Question

I'm using a macro to copy a csv file into a new worksheet, this part works well. However, my csv file uses semicolons as delimiters, and when I use the following code, the text don't split into columns:

Sub Bam()
    Dim FilesToOpen
    Dim wkbAll As Workbook
    Dim wkbTemp As Workbook
    Dim newSheet As Worksheet
    FilesToOpen = Application.GetOpenFilename _
    (FileFilter:="Text Files (*.csv), *.csv", Title:="Text Files to Open")  
    Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen)
    wkbTemp.Sheets(1).Cells.Copy
    Set newSheet = ThisWorkbook.Sheets.Add
    With newSheet
        .Name = wkbTemp.Name
        .PasteSpecial
    End With
    Application.CutCopyMode = False
    wkbTemp.Close
End Sub

I tried to use the following commands to set the semicolon as a delimiter:

With newSheet
    .Name = wkbTemp.Name
    .PasteSpecial
    .TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=True, Semicolon:=True
End With

or

Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen, Delimiter:=4)

But, in the first case, the following error message appears while I try to use my macro:

highlighting .TextToColumns

Compile error:
Method or data member not found.

In the second case, nothing happens, all the text still appears in one column.

Does anyone had an idea to set the semicolon as a delimiter?

Thank you

Was it helpful?

Solution 2

I like your way, I'll study it. In the meantime, I found a way to do it! I modified the extension of my *.csv file to *.txt, and slightly changed the code like:

Sub Bam()
    Dim FilesToOpen
    Dim wkbAll As Workbook
    Dim wkbTemp As Workbook
    Dim newSheet As Worksheet
    FilesToOpen = Application.GetOpenFilename(Title:="Text Files to Open")
        Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen, Format:=4)
        wkbTemp.Sheets(1).Cells.Copy
        Set newSheet = ThisWorkbook.Sheets.Add
        With newSheet
            .Name = wkbTemp.Name
            .PasteSpecial
        End With
        Application.CutCopyMode = False
        wkbTemp.Close   
End Sub

It's not exactly what I wanted, as I now have to modify my script which generate the *.csv file, but it works!

Thanks for your idea, I'll seriously study it, and maybe I'll not have to modify my script. In all the cases, Thanks a lot, I'm always curious about news ways to work and always want to improve my code! :) Thank you.

OTHER TIPS

I would first bring the data into column A and then parse the data out:

Sub Bam()
    Dim FilesToOpen, v As Variant
    Dim wkbAll As Workbook
    Dim wkbTemp As Workbook
    Dim newSheet As Worksheet, N As Long, J As Long
    FilesToOpen = Application.GetOpenFilename _
    (FileFilter:="Text Files (*.csv), *.csv", Title:="Text Files to Open")
    Close #1
    Open FilesToOpen For Input As #1
    J = 1
    Do While Not EOF(1)
            Line Input #1, TextLine
            Cells(J, 1) = TextLine
            J = J + 1
    Loop
    Close #1

    N = Cells(Rows.Count, "A").End(xlUp).Row
    For J = 1 To N
        v = Cells(J, 1).Value
        If InStr(v, ";") > 0 Then
            ary = Split(v, ";")
            For i = LBound(ary) To UBound(ary)
                Cells(J, i + 1).Value = ary(i)
            Next i
        End If
    Next J
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top