Opening semicolon delimited CSV file

后端 未结 8 1948
我寻月下人不归
我寻月下人不归 2020-12-18 07:57

How does one open a semicolon delimited CSV file with VBA in Excel 2000?

Sample data

An ID;TEST20090222
A Name;Firstname Surname
A D         


        
8条回答
  •  半阙折子戏
    2020-12-18 08:34

    [Update 22.02.2009]

    In the meantime, I solved the problem by writing an import function myself instead of using Workbooks.OpenText.

    I just open the CSV file as a text file, read line by line, split each line into the semicolon separated elements and write each element into a cell.

    Sub ImportCSVFile(filepath As String)
        Dim line As String
        Dim arrayOfElements
        Dim linenumber As Integer
        Dim elementnumber As Integer
        Dim element As Variant
    
        linenumber = 0
        elementnumber = 0
    
        Open filepath For Input As #1 ' Open file for input
            Do While Not EOF(1) ' Loop until end of file
                linenumber = linenumber + 1
                Line Input #1, line
                arrayOfElements = Split(line, ";")
    
                elementnumber = 0
                For Each element In arrayOfElements
                    elementnumber = elementnumber + 1
                    Cells(linenumber, elementnumber).Value = element
                Next
            Loop
        Close #1 ' Close file.
    End Sub
    

    Got the inspiration from Shasur: http://vbadud.blogspot.com/2007/06/vba-read-text-files-with-leading.html

    I still do not know why Workbooks.OpenText does not work on my system even though it seems to work on user Remou's system. I guess it might have something to do with the operating system language (English) and the regional and language settings (German, Switzerland), but I am not sure.

    Anyway, the workaround works for me. Thank you all for you suggestions and help!

提交回复
热议问题