Question

I have used some code to import a text file into Excel using ADO as shown below:

    Sub ImportTextFile()
    'Imports text file into Excel workbook using ADO.
    'If the number of records exceeds 65536 then it splits it over more than one sheet.

        Dim strFilePath As String, strFilename As String, strFullPath As String
        Dim lngCounter As Long
        Dim oConn As Object, oRS As Object, oFSObj As Object

        'Get a text file name
        strFullPath = Application.GetOpenFilename("Text Files (*.txt),*.txt", , "Please select text file...")

        If strFullPath = "False" Then Exit Sub  'User pressed Cancel on the open file dialog

        'This gives us a full path name e.g. C:\temp\folder\file.txt
        'We need to split this into path and file name
        Set oFSObj = CreateObject("SCRIPTING.FILESYSTEMOBJECT")

        strFilePath = oFSObj.GetFile(strFullPath).ParentFolder.Path
        strFilename = oFSObj.GetFile(strFullPath).Name

        'Open an ADO connection to the folder specified
        Set oConn = CreateObject("ADODB.CONNECTION")
        oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "Data Source=" & strFilePath & ";" & _
                   "Extended Properties=""text;HDR=Yes;FMT=Delimited"""

        Set oRS = CreateObject("ADODB.RECORDSET")

        'Now actually open the text file and import into Excel
        oRS.Open "SELECT * FROM [" & strFilename & "]", oConn, 3, 1, 1
        While Not oRS.EOF
            Sheets("Sheet1").Select
             ActiveSheet.Range("A1").CopyFromRecordset oRS, 65536
            Range("A1").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Range(Selection, Selection.End(xlDown)).Select
            Selection.Copy
            Range("C1").Select
            ActiveSheet.Paste
            Range("E1").Select
            ActiveSheet.Paste
            .
            .
            .
            Range("CW1").Select
            ActiveSheet.Paste
            Range("CY1").Select
            ActiveSheet.Paste

            Range("A1").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Range(Selection, Selection.End(xlDown)).Select
            Selection.Copy
            Sheets("Recorder Log").Select
            Range("A9").Select
            ActiveSheet.Paste

            Range("C8").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Application.CutCopyMode = False
            Selection.Copy
            Range("C9").Select
            Range(Selection, Selection.End(xlToRight)).Select
            Range(Selection, Selection.End(xlDown)).Select
            ActiveSheet.Paste

            Sheets("Sheet1").Select
            Cells.Select
            Selection.Delete Shift:=xlUp
            Sheets("Recorder Log").Select
            Range("C9").Select
        Wend

        oRS.Close
        oConn.Close

    End Sub

I thought the code was working perfectly but then noticed that the first line of text in the files I was importing was not getting copied into Excel.

Is there any reason why this would happen and is there a solution to avoid having to preformat the text files with a blank row at the start?

Thanks in advance.

CV

Était-ce utile?

La solution

You have set, through the connection properties, HDR=Yes, which to excel means that the first row is an header that contains column names. Since you have no header columns, you should set HDR=No instead.

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=" & strFilePath & ";" & _
               "Extended Properties=""text;HDR=No;FMT=Delimited"""
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top