创建双循环以打开工作簿,将行复制/粘贴到新打开的工作簿,然后保存/关闭新打开的工作簿

时间:2016-10-28 02:30:48

标签: excel vba loops

并提前感谢您查看/解决我的问题。让我先说一下我对VBA非常新的事实。

我正在尝试设置一个双循环,第一个将在第一个工作簿中复制一定数量的单元格(A3:K3),并将它们粘贴到单独工作簿中的特定单元格中,然后将工作簿保存为(L3)中的值,然后关闭第二个工作簿。

第二个循环(如果可能)将告诉宏重复第一个循环,在下一行(A4:K4)的相同范围内,并重复直到单元格(A(x),其中x =第一个空细胞)

我的大部分复制粘贴都是手工完成的,经过广泛的研究,我已经知道了。选择是魔鬼。我的第一个循环似乎工作正常,但由于手动操作代码的效率低下,我每次都崩溃了。

请帮我消除代码效率低下的问题,如下所示:

Dim J As Integer
Do
For J = 3 To Last_Row_In_Column_A
Workbooks.Open FileName:= _
"Desktop\Term Workbook Template.xlsx"
Windows("NBV Forecast.xlsm").Activate
Cells(J, 1).Select
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C4").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 2).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C5").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 3).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C6").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 4).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C7").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 5).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C9").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 6).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C10").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 7).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C14").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 8).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C15").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Windows("NBV Forecast.xlsm").Activate
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 9).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("C18").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 10).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("G18").Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Selection.End(xlUp).Select
Range("G5").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 11).Select
Application.CutCopyMode = False
Selection.Copy
Windows("Term Workbook Template.xlsx").Activate
Range("G11").Select
ActiveSheet.Paste
Windows("NBV Forecast.xlsm").Activate
Cells(J, 12).Select
Dim Path As String
Dim FileName As String
Path = "Desktop\"
FileName = Range("L12")
Windows("Term Workbook Template.xlsx").Activate
ActiveWorkbook.SaveAs FileName:=Path & FileName & ".xlsx"
ActiveWorkbook.Close
Next J
Loop

End Sub

....我知道这很可能是一团糟,我为此给您带来的任何不便表示歉意。我很感激能得到的任何帮助!

1 个答案:

答案 0 :(得分:0)

如果你检查你的根本问题它基本上是这个问题的副本,你应该找到你在这里或那里的答案

从一个工作簿复制并粘贴到另一个工作簿

https://stackoverflow.com/a/19352099/6868389