我想问一下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
我想选择范围内的所有数据" C2:E2"到" C9:E9"并将其粘贴到其他工作簿。
在我目前的代码中,我需要从C2到C9逐个输入。 我想在循环中从C2-C9中选择数据。
有没有办法在循环中做到这一点?
答案 0 :(得分:1)
您可以使用Range("C2:E9").Copy
复制整个范围。
此外,无需使用Select
,Activate
和Selection
,这会降低代码运行时间,只需使用完全qulifed Wroksheets
和{{ 1}}而不是。
<强>代码强>
Range
答案 1 :(得分:0)
而是使用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