【问题标题】:Copying text from .txt file in Excel using ADO ignores first row使用 ADO 从 Excel 中的 .txt 文件复制文本会忽略第一行
【发布时间】:2013-06-03 13:35:01
【问题描述】:

我使用了一些代码,使用 ADO 将文本文件导入 Excel,如下所示:

    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

我认为代码运行良好,但后来注意到我正在导入的文件中的第一行文本没有被复制到 Excel 中。

有什么原因会发生这种情况吗?是否有解决方案可以避免在开始时使用空白行预先格式化文本文件?

提前致谢。

简历

【问题讨论】:

  • 原始文本文件中第一行的内容是什么 - 有列标题还是里面没有列标题??
  • 所以,试着在你的代码中把这个:HDR=Yes 改成这个HDR=No...
  • 那行得通。谢谢卡兹乔。我从另一个用户那里获取了代码,但没有意识到这是 HDR 所做的。

标签: excel vba import ado jet


【解决方案1】:

您已通过连接属性设置HDR=Yes,这意味着第一行是包含列名的标题。由于您没有标题列,因此您应该设置 HDR=No

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=" & strFilePath & ";" & _
               "Extended Properties=""text;HDR=No;FMT=Delimited"""

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2021-07-29
    • 2014-09-08
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多