0
我必須複製從工作表阿行到工作表B.然而,我在交替行粘貼,如:複製一個工作表並粘貼到另一備用行中(Excel的VBA)
1 st row(Sheet A)-- 1 st row (Sheet B)
2nd row (Sheet A) --> 3rd row (Sheet B)
3rd row (Sheet A)--> 5th row(Sheet B)
這可能嗎?
感謝您的幫助。
我必須複製從工作表阿行到工作表B.然而,我在交替行粘貼,如:複製一個工作表並粘貼到另一備用行中(Excel的VBA)
1 st row(Sheet A)-- 1 st row (Sheet B)
2nd row (Sheet A) --> 3rd row (Sheet B)
3rd row (Sheet A)--> 5th row(Sheet B)
這可能嗎?
感謝您的幫助。
你可以試試這樣的事情...
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
我很抱歉,但這並沒有奏效。 – Sona123
什麼沒有工作?你是否能夠理解代碼,以便在需要時調整它。代碼非常簡單。它將所有數據從Sheet1複製到Sheet2,然後在Sheet2之間插入一行。或者在Sheet2上,你已經有了一些你不想用Sheet1數據替換的偶數行數據? – sktneer
你好,它確實聽起來正是我所需要的。但是,當我運行這段代碼似乎進入了一個無限循環 - 沒有給出錯誤沒有得到工作表更新或者Excel工作表變成「沒有響應」。是否有任何原因 – Sona123