从TXT文件创建的VBA记录集返回Null值(Excel 2013)

时间:2018-05-15 18:11:44

标签: excel vba excel-vba ado recordset

我是在VBA中使用记录集的新手,这个问题让我发疯。

我收到一份大报告(cca 57000行),保存为带有标签的文本文件。我的任务是删除不必要的数据,插入其他数据并创建像数据透视表这样的东西。现在这个任务是在Excel中手动完成的,它产生一个大文件,使用起来不舒服。所以我想在记录集中导入TXT文件并使用SQL语句操作数据,并将结果保存在单独的Excel文件中。

我已成功直接从TXT导入数据。 .CountRecord显示正确的记录数,但它们是空的。 我尝试从.Execute.Open创建记录集,结果相同。 这是我的VBA代码:

Sub user_statistic_report()

Dim sPath As String
Range("A1").EntireRow.Delete

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
With cn
    .Provider = "Microsoft.Jet.OLEDB.4.0;"
    .Properties("Data Source") = "C:\\Mydocs\raport\" 'sample path
    .Properties("Extended properties") = "text;HDR=No;FMT=Delimited;"
    .Open
End With

rs.CursorLocation = 3 'someone told it helped him but it did not a trick for me
'later I'd like to select only rows with specific conditions
rs.Open "SELECT * FROM [b.txt]", cn, 3
Debug.Print "Number of records:", rs.RecordCount

Set dbFields = rs.Fields
For i = 0 To dbFields.Count - 1
    Debug.Print "Column #", i, dbFields.Item(i).Name
Next i

Debug.Print "Recordset item", rs(3)

rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing

End Sub

以下是我在立即窗口中看到的内容:

Number of records:           5<BR> 
Column #       0            F1<BR>
Column #       1            F2<BR>
Column #       2            F3<BR>
Column #       3            F4<BR>
Recordset item              Null<BR>

以下是我的示例TXT文件 b.txt 。每一行都以标签开头:

    Col1    Col2    Col3
    11  12  13
    21  22  23

这是 schema.ini

[b.txt]
Format=TabDelimited
ColNameHeader=False
MaxScanRows=0

请问您为什么记录集会返回Null值?

更新 问题出在我的TXT文件中,它有点被破坏了。我已经在文件中重新输入了值,脚本现在正在运行。

1 个答案:

答案 0 :(得分:0)

我假设您还想打印字段的值而不仅仅是字段名称。

修改你的代码

Set dbFields = rs.Fields
For i = 0 To dbFields.Count - 1
    Debug.Print "Column #", i, dbFields.Item(i).Name
Next i


rs.MoveFirst
Do While Not rs.EOF
    Debug.Print rs.Fields(1).Value, rs.Fields(2).Value, rs.Fields(3).Value
    rs.MoveNext
Loop

下面的部分将打印字段的值。

您的问题是您误解了行的输出

Debug.Print "Recordset item", rs(3)

此行打印字段编号3的值。最好编写

Debug.Print "Recordset item", rs.fields(3).value

此字段可能包含Null

更新您可以尝试以下代码。您需要添加reference to ADODB

Option Explicit

Sub user_statistic_report()

Dim sPath As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0;"
        .Properties("Data Source") = "C:\\Mydocs\raport\"    'sample path
        .Properties("Extended properties") = "text;HDR=No;FMT=Delimited;"
        .Open
    End With

    rs.CursorLocation = adUseClient    'someone told it helped him but it did not a trick for me
    'later I'd like to select only rows with specific conditions
    rs.Open "SELECT * FROM [b.txt]", cn, adOpenStatic
    Debug.Print "Number of records:", rs.RecordCount

    Dim wks As Worksheet
    Set wks = ActiveSheet

    wks.UsedRange.Clear
    WriteToSheet wks, rs

    rs.Close
    cn.Close
    Set cn = Nothing
    Set rs = Nothing

End Sub


Sub WriteToSheet(ByVal sh As Worksheet, ByVal rs As ADODB.Recordset)

Dim dbfields As ADODB.Fields
Dim i As Long, j As Long

    ' Write Heading resp. Field names in row 1
    Set dbfields = rs.Fields
    For i = 0 To dbfields.Count - 1
        sh.Cells(1, i + 1).Value = dbfields.Item(i).Name
    Next i

    ' Write values of the recordset starting at row 3
    j = 2
    rs.MoveFirst
    Do While Not rs.EOF
        For i = 0 To dbfields.Count - 1
            sh.Cells(j, i + 1).Value = rs.Fields.Item(i).Value
        Next i
        rs.MoveNext
        j = j + 1
    Loop

End Sub

更新2 这是文本文件

enter image description here

这是十六进制视图

enter image description here

这就是excel使用脚本

读取文件的结果

enter image description here

因此,该文件的内容是

TAB 11 TAB TAB 13 CRLF

TAB 21 TAB 22 TAB 23 CRLF

TAB 31 TAB 32 TAB 33 CRLF

TAB TAB 42 TAB 43 CRLF