2017-07-27 200 views

回答

0

你可以試試這樣的事情...

Sub CopyRows() 
Dim sws As Worksheet, dws As Worksheet 
Dim i As Long, lr As Long 
Application.ScreenUpdating = False 
Set sws = Sheets("Sheet1") 
Set dws = Sheets("Sheet2") 
lr = sws.UsedRange.Rows.Count 
dws.Cells.Clear 
sws.UsedRange.Copy dws.Range("A1") 
For i = lr To 1 Step -1 
    dws.Rows(i).Insert 
Next i 
Application.ScreenUpdating = True 
End Sub 

編輯答案:

你可以試試這個方法,這將是更快足以從Sheet1的數據複製到Sheet2中。唯一的缺點是它將只複製Sheet1上的任何公式的值。

Sub CopyRows() 
Dim sws As Worksheet, dws As Worksheet 
Dim i As Long, ii As Long, j As Long 
Dim x, y() 
Application.ScreenUpdating = False 
Set sws = Sheets("Sheet1") 
Set dws = Sheets("Sheet2") 
dws.Cells.Clear 
x = sws.Range("A1").CurrentRegion.Value 
ReDim y(1 To UBound(x, 1) * 2, 1 To UBound(x, 2)) 
For i = 1 To UBound(x, 1) 
    j = j + 2 
    For ii = 1 To UBound(x, 2) 
     y(j, ii) = x(i, ii) 
    Next ii 
Next i 
dws.Range("A1").Resize(UBound(y, 1), UBound(y, 2)).Value = y 
End Sub 
+0

我很抱歉,但這並沒有奏效。 – Sona123

+0

什麼沒有工作?你是否能夠理解代碼,以便在需要時調整它。代碼非常簡單。它將所有數據從Sheet1複製到Sheet2,然後在Sheet2之間插入一行。或者在Sheet2上,你已經有了一些你不想用Sheet1數據替換的偶數行數據? – sktneer

+0

你好,它確實聽起來正是我所需要的。但是,當我運行這段代碼似乎進入了一個無限循環 - 沒有給出錯誤沒有得到工作表更新或者Excel工作表變成「沒有響應」。是否有任何原因 – Sona123

相關問題