2015-09-25 70 views
0

我有興趣將一個工作簿的大範圍複製到另一個工作簿。目前,我正在使用複製和粘貼功能。宏處理較小的文件,但現在我有一個非常大的文件,我遇到了一個錯誤ActiveSheet.Paste。我認爲這是因爲我拷貝的剪貼板太大了。這聽起來是對的嗎?如果是這樣,我想避免剪貼板一起。這是我的代碼目前。將大範圍複製到另一個工作簿時避免剪貼板?

DeptReceivedWB.Sheets(1).Cells(1, 1).CurrentRegion.Copy 


APPS_AuditWB.Activate 
APPS_AuditWB.Sheets(3).Select 
ActiveSheet.Cells(i + 1, 1).Select 
ActiveSheet.Paste 
ActiveSheet.Cells(i + 1, 1).EntireRow.Delete 
+0

如果您不需要格式化,只需將一個範圍設置爲另一個。避免複製/粘貼在一起。 – findwindow

回答

1

使用下列操作之一:

Option Explicit 

Public Sub valuesCopy() 
    With DeptReceivedWB.Sheets(1).Cells(1, 1).CurrentRegion 
     .Offset(1).Resize(.Rows.Count - 1, .Columns.Count).Copy 
    End With 

    APPS_AuditWB.Sheets(3).Cells(1, 1).PasteSpecial xlPasteValues 
End Sub 

Public Sub directCopy1() 
    Dim cr As Range, fr As Long, lr As Long, fc As Long, lc As Long 

    Set cr = DeptReceivedWB.Sheets(1).Cells(1, 1).CurrentRegion 
    fr = cr.Row: lr = fr + cr.Rows.Count - 2 
    fc = cr.Column: lc = fc + cr.Columns.Count - 1 
    Set cr = cr.Offset(1).Resize(lr, lc) 

    With APPS_AuditWB.Sheets(3) 
     .Range(.Cells(fr, fc), .Cells(lr, lc)).Value2 = cr.Value2 
    End With 
End Sub 

Public Sub directCopy2() 
    Dim cr As Range, fr As Long, lr As Long, fc As Long, lc As Long 

    Set cr = DeptReceivedWB.Sheets(1).Cells(1, 1).CurrentRegion 
    fr = cr.Row: lr = cr.Rows.Count - 1 
    fc = cr.Column: lc = cr.Columns.Count 
    Set cr = cr.Offset(1).Resize(lr, lc) 

    With APPS_AuditWB.Sheets(3) 
     .Cells(fr, fc).Resize(lr, lc).Value2 = cr.Value2 
    End With 
End Sub 
+1

錯字 - 'lc'沒有賦值。但這隻適用於複製範圍從A1開始 - 也許更好使用'.Cells(fr,fc).Resize(lr,lc).Value2 = ur.Value2'? –

+0

謝謝@Tim - 沒有測試就發生了什麼... –

+0

謝謝@paul bica,我使用了directCopy1,它的功能就像一個魅力!這是我第一次看到Value2。我認爲Value2是一個範圍的值,其中普通舊值僅適用於單個單元格? –

0

我會建議你只想要複製的範圍內,而不是複製(大概)標題行,然後從目標刪除它:

Dim rgCopy as range 
set rgCopy = DeptReceivedWB.Sheets(1).Cells(1, 1).CurrentRegion 
set rgCopy = rgCopy.Resize(rgCopy.Rows.Count - 1).Offset(1) 
rgCopy.Copy Destination:=APPS_AuditWB.Sheets(3).Cells(i + 1, 1) 
相關問題