Opening semicolon delimited CSV file

后端 未结 8 1932
我寻月下人不归
我寻月下人不归 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:26

    One more workaround - just rename .csv files to .txt and use OpenText method.

    0 讨论(0)
  • 2020-12-18 08:26

    Re Any ideas?

    If you want to fix a file for others using excel add this on the first line of the file without the quotes followed by a linebreak: "sep=;”

    An easy way to manually open stupid files is to rename the extension to .txt or .htm then from excel use File - Open.

    From VBA I recommend looking up the method in MSDN and manually specifying every parameter, my experience was that this eliminated most regional issues.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-18 08:36

    I prefer:

    Workbooks.Open fileName:=myFilename, UpdateLinks:=False, Local:=True
    
    0 讨论(0)
  • 2020-12-18 08:37

    often the comma is ticked for true as a separator where it is usually the decimal separator. Add DecimalSeparator:="," and Bob's your uncle

    0 讨论(0)
  • 2020-12-18 08:39

    Not sure, but you can try recording a macro to do the same thing and check the VBA code it produces. You might get a hint there as to what's missing.

    0 讨论(0)
提交回复
热议问题