2016-12-06 8 views
0

ボタンを押すと、あるシートの特定のデータが別のシートにコピーされます。このコードは機能します。すべての行がコピーされていますが、このマクロは特定の範囲で動作します。私は、このマクロは、列Yに、列Aから別のシートが、データにない行全体をコピーしたい特定の列範囲でマクロを動作させる方法。 EXCEL

私のコード:次のコードは、ちょうど列が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

常にシート間を切り替える必要性を取り除くためにリファクタリングしました。助けのための

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 
+0

感謝 –