2014-10-16 24 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 =「我的字符串」編寫進度消息可能會有所幫助。