2016-08-24 104 views
-1

我有一個非常大的CSV文件,有60列和50k行。 (我不能告訴你,因爲它是在企業內部網)導入大型CSV時Excel凍結

我寫的VBA代碼,這是否:

Columns("D:BF").Select 
Selection.ClearContents 
*Code to import csv file to D1* 
LastRow = Cells(Rows.Count, "D").End(xlUp).Row 
Range("A1:C1").Select 
Selection.Copy 
Range("A2:C" & lastrow).Select 
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _ 
    SkipBlanks:=False, Transpose:=False 

這是工作,但已停止工作。當我運行宏時,Excel會凍結。我需要按「Esc」,宏停止,然後調試器說PasteSpecial是錯誤的。 但它不是,如果我只運行一個宏來導入,然後按「Esc」,它會導入得很好,然後運行第二個宏來粘貼公式直到最後一行,它也運行得非常好。

工作簿是人工計算

http://i.stack.imgur.com/HKzJE.jpg

+0

#felipe31你確定你的代碼?!?!?你選擇單元格(「D:BF」),並清除那些,那麼LastRow將是D1! – Fabrizio

+0

@Fabrizio OP在導入新數據之前清除舊數據。 – 2016-08-24 06:21:51

+0

@ThomasInzina是對的 – felipe31

回答

1

逐行讀取CSV文件一行到一個數組是10,000行通過100列。當數組已滿時,我將它寫入Excel,重新設置數組並繼續,直到文件結束。

我最初嘗試使用動態數組,但它會凍結Excel。使用靜態數組實際上更有效。

您需要調整常數值。從你的圖像看起來你的文件是分號分隔的。如果ColumnCount只要大於或等於實際列數,那麼確實如此。

Const CSVFileName = "C:\Users\best buy\Downloads\stackoverfow\Sample Data File\R58K x C60.csv" 
Const Delimiter = "," 
Const PageSize = 10000 
Const ColumnCount = 100 

導入100,000行和64列大約需要32秒。 CSV文件在磁盤上爲69.5 MB。

enter image description here

Option Explicit 

Sub ProcessFile() 
    Const CSVFileName = "C:\Users\best buy\Downloads\stackoverfow\Sample Data File\R58K x C60.csv" 
    Const Delimiter = "," 

    ActiveSheet.DisplayPageBreaks = False 
    With Application 
     .ScreenUpdating = False 
     .Calculation = xlCalculationManual 
    End With 

    Debug.Print Now 
    Dim Start: Start = Timer 

    Dim lastRow As Long 
    Dim arFormulas 
    arFormulas = Range("A1:C1").Formula 

    Columns("A:BF").ClearContents 

    ImportCSVFile CSVFileName, Delimiter 

    Debug.Print "Time to import CSV file in seconds:"; Timer - Start 
    Start = Timer 

    lastRow = Cells(Rows.Count, "D").End(xlUp).Row 

    Range("A1:C" & lastRow).Formula = arFormulas 

    Debug.Print "Time to add formulas in seconds:"; Timer - Start 

    Debug.Print "Column Count:"; Worksheets("Sheet1").UsedRange.Columns.Count 
    Debug.Print "Row Count:"; Worksheets("Sheet1").UsedRange.Rows.Count 

    With Application 
     .ScreenUpdating = True 
     .Calculation = xlCalculationAutomatic 
     .AutoRecover.Enabled = True 
    End With 
End Sub 

Sub ImportCSVFile(FilePath As String, Delimiter As String) 
    Const PageSize = 10000 
    Const ColumnCount = 100 

    Dim line As String 
    Dim arData, arLine 
    Dim x As Long, y As Long, z As Long 
    ReDim arData(PageSize, ColumnCount) 
    z = 1 

    Open FilePath For Input As #1      ' Open file for input 
    Do While Not EOF(1)        ' Loop until end of file 
     Line Input #1, line 
     arLine = Split(line, Delimiter) 

     y = 0 
     For y = 0 To UBound(arLine) 
      arData(x, y) = arLine(y) 
     Next 
     x = x + 1 

     If x = PageSize Or EOF(1) Then 

      Range("D" & z).Resize(x, y) = arData 
      z = z + x 
      ReDim arData(PageSize, ColumnCount) 
      x = 0 
     End If 

    Loop 

    Close #1 

    Erase arData 
End Sub 
+0

明天我會試試這個,我告訴你 – felipe31

+0

它不起作用,問題在於導入。我做了一個宏只是爲了導入,它一直凍結,我需要按esc – felipe31

+0

如果「問題在於導入」,那麼你應該發佈此代碼:「*代碼將CSV文件導入到D1 *」 – 2016-08-24 20:42:12

0

使用粘貼後doevents命令我希望工程