2014-10-16 63 views
0

我有一个文件夹包含测试数据的.txt文件。我编写了一个宏来筛选.txt文件,根据一些搜索条件提取我想要的信息,然后将这些搜索结果写入二进制文件。从二进制文件导入数据非常慢

因此,现在我有一个包含简化数据集的二进制文件,我编写了另一个宏来搜索二进制文件以获得我真正想要的内容。

出于某种原因,我的宏从二进制文件中读取数据的速度很慢。

作为比较,我写了一个宏,查看所有txt文件中的特定搜索,将其写入二进制文件,然后将其读回Excel。那只需要60秒。

这是一段代码。我想知道if-else语句是否符合我的搜索条件[LC(a)和EID(e)]是否会降低速度或减小二进制文件大小(仅为200 MB)。

Type MyBinaryRecordInfo 
    MyBinaryRecordInfo1(1 To 12) As String ' variable length 
End Type  

i = 1 
Open currentpath & "\" & bin_fname & ".DAT" For Binary As #f 
' read records from the binary file 
For a = 1 To totalLC 
    For e = 1 To totalElm 
     Do While Loc(f) < LOF(f) 
      Call ReadBinRecord(MyRecord, f, ElmType) 
      Sheets(ElmType).Select 
      If MyRecord.MyBinaryRecordInfo1(1) = LC(a) Then 
       If MyRecord.MyBinaryRecordInfo1(2) = EID(e) Then 
        For j = 1 To totalbinrec 
         With MyRecord 
          Cells(i + 3, j) = .MyBinaryRecordInfo1(j) 
         End With 
        Next j 
        i = i + 1 
        Exit Do 
       End If 
      End If 
     Loop 
    Next e 
Next a 
Close #f ' close the file 

Sub ReadBinRecord(MyRecord As MyBinaryRecordInfo, f As Integer, ElmType As String) 
' reads the next record from an open binary file 
Dim intSize As Integer 

For j = 1 To totalbinrec 
    With MyRecord 
     Get f, , intSize ' read the size of the ID field 
     .MyBinaryRecordInfo1(j) = String(intSize, " ") ' set the variable length 
     Get f, , .MyBinaryRecordInfo1(j) ' read the variable string field 
    End With 
Next j 

回答

0

可能缓慢的部分写出到Excel中。特别是,每次读取行时更改表格

假设您事先知道二进制文件中的行数,可以读入内存,然后在末尾写出一次。代码将如下所示:

Option Explicit 

Type MyBinaryRecordInfo 
    MyBinaryRecordInfo1(1 To 12) As String ' variable length 
End Type 

Sub x() 
num_rows = 5000 
Open currentpath & "\" & bin_fname & ".DAT" For Binary As #f 
Dim V(1 To totalElm) As Variant 
Dim V2(1 To num_rows, 1 To 12) As Variant 
For e = 1 To totalElm 
    V(e) = V2 
Next e 
' read records from the binary file 
For a = 1 To totalLC 
    For e = 1 To totalElm 
     Do While Loc(f) < LOF(f) 
      Call ReadBinRecord(MyRecord, f, ElmType) 
      If MyRecord.MyBinaryRecordInfo1(1) = LC(a) Then 
       If MyRecord.MyBinaryRecordInfo1(2) = EID(e) Then 
        For j = 1 To totalbinrec 
         With MyRecord 
          V(e)(i, j) = .MyBinaryRecordInfo1(j) 
         End With 
        Next j 
        i = i + 1 
        Exit Do 
       End If 
      End If 
     Loop 
    Next e 
Next a 
Close #f ' close the file 

' write out 
For e = 1 To totalElm 
    Sheets(ElmType).Select 
    Cells(3, 1).Resize(num_rows, 12).Value = V(e) 
Next e 
End Sub 

Sub ReadBinRecord(MyRecord As MyBinaryRecordInfo, f As Integer, ElmType As String) 
' reads the next record from an open binary file 
Dim intSize As Integer 

For j = 1 To totalbinrec 
    With MyRecord 
     Get f, , intSize ' read the size of the ID field 
     .MyBinaryRecordInfo1(j) = String(intSize, " ") ' set the variable length 
     Get f, , .MyBinaryRecordInfo1(j) ' read the variable string field 
    End With 
Next j 
End Sub 

如果您不知道行数,那么您只需Redim每500行左右保留一次内部变量。因为这样会在最后写出所有内容,所以使用Application.Statusbar =“我的字符串”编写进度消息可能会有所帮助。