当我按下按钮时,一张纸上的特定数据被复制到另一张纸上。这段代码有效。所有行都在复制,但我希望此宏在特定范围内工作。我希望这个宏不是将整行复制到另一个工作表,而是将数据从A列复制到Y列。
我的代码:
Sub Button4_Click()
x = 15
Do While Cells(x, 1) <> ""
If Cells(x, 1) <> "" Then
Worksheets("Sheet1").Rows(x).Copy
Rows(x).PasteSpecial xlPasteValues
Worksheets("Sheet2").Activate
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet2").Rows(erow)
End If
Worksheets("Sheet1").Activate
x = x + 1
Loop
End Sub
答案 0 :(得分:0)
以下代码只会将A列复制到Y.我还重构了它以消除不断在工作表之间切换的需要。
Sub Button4_Click()
Dim x As Long
Dim erow as Long
'Calculate starting rows
x = 15
With Worksheets("Sheet2")
erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
End With
With Worksheets("Sheet1")
Do While .Cells(x, 1) <> ""
'Your current code replaces the source data with its values so
'I have left that functionality in by using the following line
.Range("A" & x & ":Y" & x).Value = .Range("A" & x & ":Y" & x).Value
'The next line copies values to Sheet2
Worksheets("Sheet2").Range("A" & erow & ":Y" & erow).Value = .Range("A" & x & ":Y" & x).Value
'increment row counters
x = x + 1
erow = erow + 1
Loop
End With
End Sub