使用VBA将多个文本文件中的一条记录只读到Excel中

时间:2016-08-03 22:23:21

标签: excel vba excel-vba

我在一个文件夹中有多个txt文件,这些文件是制表符分隔的。这些文件中的每一个都有一个名为EngagementId的列,它与值相同,与记录数无关。但是,它会针对每个txt文件进行更改,这是我想要捕获的内容。

  1. 我想在第一行获取文件名。 GetFileNames()适用于此(如评论中所指出的)
  2. Sub GetFileNames()
        Dim sPath As String
        Dim sFile As String
        Dim iRow As Integer
        Dim iCol As Integer
        Dim splitFile As Variant
    
        'specify directory to use - must end in "\"
        sPath = ActiveWorkbook.Path
        iRow = 0
        sFile = Dir(sPath & "\Individual Reports\")
        Do While sFile <> ""
            iRow = iRow + 1
            splitFile = Split(sFile, ".txt")
            For iCol = 0 To UBound(splitFile)
                Sheet1.Cells(iRow, iCol + 1) = splitFile(iCol)
            Next iCol
            sFile = Dir     ' Get next filename
        Loop
    End Sub
    

    这些txt文件中的每一个都有一列(在每个文本文件中位于第13个位置),称为&#34; EngagementId&#34;。我想只提取第一个&#34; Engagement Id&#34;,它来自第二行(因为第一行包含标题)。

    Sub Extractrec()
        Dim filename As String, nextrow As Long, MyFolder As String
        Dim MyFile As String, text As String, textline As String
    
        MyFolder = ActiveWorkbook.Path
        MyFile = Dir(MyFolder & "\Individual Reports\*.txt")
    
        Do While MyFile <> ""
            Open (MyFolder & MyFile) For Input As #1
            Do Until EOF(1)
                Line Input #1, LineFromFile
                LineItems = Split(LineFromFile, "\t") 'second loop text is already stored 
                                                      '-> see reset text
                Sheet1.Cells(iRow, iCol + 2).Value = LineItems(13, 2)
            Loop
            Close #1
    
        Loop
    

2 个答案:

答案 0 :(得分:1)

使用ADODB.Recordset进行查询会更加通用。

&#xA;&#xA;
&#xA;&#xA;
 子示例()&# XA; On Error Resume Next&#xA; Dim rs As Object,f As Object,conn As Object&#xA; Dim FolderPath As String,FileName As String,FilterString As String&#xA; FolderPath =“C:\ Users \ best buy \ Downloads \ stackoverfow \ Sample Data File \”&#xA; FileName =“example.csv”&#xA; FilterString =“WHERE EngagementId = 20”&#xA;&#xA;设置rs = getDataset(FolderPath,FileName,FilterString)&#xA;&#xA;做的不是rs.BOF而不是rs.EOF&#xA; Debug.Print rs.Fields(“EngagementId”)&#xA; Debug.Print rs.Fields(“Company”)&#xA; Debug.Print rs.Fields(“City”)&#xA; Debug.Print rs.Fields(“State”)&#xA;&#xA; rs.MoveNext&#XA;环&#XA;&#XA;设置conn = rs.ActiveConnection&#xA; rs.Close&#XA; conn.Close&#XA;设rs = Nothing&#xA;设置conn = Nothing&#xA; End Sub&#xA;&#xA;函数getDataset(FolderPath As String,FileName As String,FilterString As String)As Object&#xA; Dim conn As Object,rs As Object&#xA;设置conn = CreateObject(“ADODB.Connection”)&#xA;设置rs = CreateObject(“ADODB.Recordset”)&#xA; conn.Open(“Provider = Microsoft.Jet.OLEDB.4.0; Data Source =”&amp; FolderPath&amp;“;”&amp; _&#xA;“Extended Properties =”“text; HDR = Yes; FMT = Delimited; IMEX = 1; “”“)&#XA; rs.ActiveConnection = conn&#xA; rs.Source =“SELECT * FROM”&amp; FileName&amp; “”&amp; FilterString&#XA; rs.Open&#XA;设置getDataset = rs&#xA;结束函数&#xA;  
&#xA;

答案 1 :(得分:0)

由于你只需要每个文件的第二行,你不需要循环,只需读取并丢弃第一行,然后阅读并拆分第二行:

    Open (MyFolder & MyFile) For Input As #1 'MyFolder & MyFile won't be the correct name (probably should be MyFolder & "\Individual Reports\" & MyFile)
    Line Input #1, LineFromFile 'line to discard
    Line Input #1, LineFromFile 'line to use
    LineItems = Split(LineFromFile, vbTab)
    Sheet1.Cells(someplace).Value = LineItems(13) ' replace some place with the correct value that we don't know
    Close #1