将大数据从文本文件导入Excel

时间:2017-09-19 16:57:24

标签: excel vba excel-vba

我有下一个问题,我需要比较一个大约127,000行的excel文件和一个大约1' 500,000行的文本文件。文本文件中的每一行都被八个管道划分,在excel中有8列。我需要在文本行中的第五列使用excel文件中的某些列执行VLookUp。

我一直在做的是:

  1. 我取excel文件中的第一行,我在文本文件中查找它,我只导入该行,然后我在excel的各自列中放入行,最后我执行VLookUp。
  2. Sub test()
    Dim textline As String, rw As Long, lRow As Long, rw1 As Long
    Dim sPath As String, text As Variant
    
    rw1 = 1
    sPath = "My_path"
    
    With ActiveWorkbook.Sheets("Sheet1")
       For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
           text = range("A" & rw).Value2
           Open sPath For Input As #1
           Do Until EOF(1)
               Line Input #1, textline
                 If InStr(textline, CStr(text)) Then
                   Sheets(2).Cells(rw1, 1) = textline
                   Sheets(2).range("A" & rw1).TextToColumns Destination:=range("A" & rw1 & ":H" & rw1), DataType:=xlDelimited, _
                       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                       Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
                       :="|", TrailingMinusNumbers:=True
                   rw1 = rw1 + 1
                   Exit Do
               End If
           Loop
           Close #1
       Next rw
    End With
    End Sub
    
    1. 我将所有文本文件导入excel,然后我将这条线放在各自的列中,最后我做了VLookUp。
    2. Sub test()
      Dim textline As String, rw As Long, lRow As Long, rw1 As Long
      Dim sPath As String, text As Variant, sh As String, c As Integer
      
      sh = "Sheet"
      c = 1
      rw = 1
      rw1 = 1
      sPath = "My_path"
      
      
      With ActiveWorkbook.Sheets("Sheet1")
             Open sPath For Input As #1
             Do Until EOF(1)
                 Line Input #1, textline
                   Sheets("Sheet1").range("A" & rw) = textline
                     Exit Do
                   End If
                   rw = rw + 1
             Loop
             Close #1
      End With
      
      With ActiveWorkbook.Sheets(1)
         For rw1 = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
             Sheets("Sheet1").range("A" & rw1).TextToColumns Destination:=range("A" & rw1 & ":H" & rw1), DataType:=xlDelimited, _
                 TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                 Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
                 :="|", TrailingMinusNumbers:=True
         Next rw1
      End With
      End Sub
      Now
      

      问题是这两种方法耗时太长,有没有人知道一个更好的解决方案可以帮助我解决这个问题?

      谢谢你的建议

1 个答案:

答案 0 :(得分:0)

由于您超出了正常的行限制,因此我建议您使用Get & Transform(适用于Excel 2016)或Power Query(适用于Excel 2010和2013)。重新设置使用Excel。