循环中的VBA复制select范围

我想问一下VBA中的循环。 我有这个VBA代码:

Sub OpenCopyPaste() ' open the source workbook and select the source sheet Workbooks.Open Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx" Sheets("Sheet1").Select ' copy the source range Sheets("Sheet1").Range("C2:E2,C3:E3,C4:E4,C5:E5,C6:E6,C7:E7,C8:E8,C9:E9").Select Selection.Copy ' select current workbook and paste the values starting at A1 Windows("report.xlsx").Activate Sheets("Sheet1").Select Sheets("Sheet1").Range("C3").Select ActiveSheet.Paste Application.CutCopyMode = False ActiveWorkbook.Save End Sub 

我想select范围“C2:E2”到“C9:E9”中的所有数据并将其粘贴到其他工作簿。

在我现在的代码中,我需要从C2 – C9中逐个input。 我想在循环中从C2-C9中select数据。

有没有什么办法可以做到这一点?

您可以使用Range("C2:E9").Copy复制整个范围。

另外,不需要使用SelectActivateSelection ,它会减慢代码的运行时间,只需使用完全可修改的WroksheetsRange

 Option Explicit Sub OpenCopyPaste() Dim wb As Workbook Dim Reportwb As Workbook ' set report workbook to workbook object (works only is the file is already open) Set Reportwb = Workbooks("report.xlsx") ' open the source workbook and select the source sheet Set wb = Workbooks.Open(Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx") ' copy the source range and paste in 1 line , paste at "C3" wb.Sheets("Sheet1").Range("C2:E9").Copy Destination:=Reportwb.Sheets("Sheet1").Range("C3") Application.CutCopyMode = False Reportwb.Save End Sub 

而是使用C2:E9

 Sub OpenCopyPaste() ' open the source workbook and select the source sheet Workbooks.Open Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx" Sheets("Sheet1").Select ' copy the source range Sheets("Sheet1").Range("C2:E9").Select Selection.Copy ' select current workbook and paste the values starting at A1 Windows("report.xlsx").Activate Sheets("Sheet1").Select Sheets("Sheet1").Range("C3").Select ActiveSheet.Paste Application.CutCopyMode = False ActiveWorkbook.Save End Sub