简体   繁体   English

从CSV复制大数据到Excel

[英]Copying the big data from csv to excel

Public Function Import_CSV (SheetName) As String

    Dim ws As Worksheet, strFile As String
    Set ws = ActiveWorkbook.Sheets(SheetName) 'set to current worksheet name
    strFile = Application.GetOpenFilename("CSV Files (*.csv),*.csv", , "Please selec .csv file...")

    Dim wbkS As Workbook
    Dim wshS As Worksheet
    Dim wshT As Worksheet
    Set wbkS = Workbooks.Open(Filename:=strFile)
    Set wshS = wbkS.Worksheets(1)
    LastRow = wshS.UsedRange.Rows.Count
    LastCol = wshS.UsedRange.Columns.Count

    With ws
        For i = 1 To LastRow Step 1
            For j = 1 To LastCol Step 1
                .Cells(i, j).Value = wshS.Cells(i, j)
            Next
        Next
    End With
    wbkS.Close

    ActiveWorkbook.Save

End Function

i tried the above code to copy the data from .csv file to excel sheet. 我尝试了上面的代码将数据从.csv文件复制到Excel工作表。 but my csv file is big (2000 rows and 2000 columns). 但我的csv文件很大(2000行和2000列)。 its taking lot of time. 这需要很多时间。 is there any other way i can copy the data to excel without much time. 有没有其他方法可以将数据复制到Excel中而无需花费很多时间。

Please help me on this issue. 请帮我解决这个问题。

If the need is copying the values from one worksheet to another but saving the formats of the destination worksheet, then you can either assign the whole values array from the source range to the destination range. 如果需要将值从一个工作表复制到另一个工作表,但保存目标工作表的格式,则可以将整个值数组从源范围分配到目标范围。

Example: 例:

Sub test()

 Set ws = Worksheets(2)
 Set wshS = Worksheets(1)

 LastRow = 2000
 LastCol = 2000

 ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, LastCol)).Value = wshS.Range(wshS.Cells(1, 1), wshS.Cells(LastRow, LastCol)).Value

End Sub

With this you may run into memory issues with very big arrays. 这样,您可能会遇到非常大的阵列的内存问题。

Or you could use the clipboard and then paste special only the values. 或者,您可以使用剪贴板,然后仅粘贴特殊的值。

Example: 例:

Sub test2()

 Set ws = Worksheets(2)
 Set wshS = Worksheets(1)

 LastRow = 2000
 LastCol = 2000

 wshS.Range(wshS.Cells(1, 1), wshS.Cells(LastRow, LastCol)).Copy
 ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, LastCol)).PasteSpecial Paste:=xlPasteValues

 Application.CutCopyMode = False

End Sub

This is even faster for me but this could be a subjective impression. 这对我来说甚至更快,但这可能是一个主观印象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM