我的代码将遍历我的工作表,但它会执行所有行的复制,而不仅仅是基于我的标准的行。我怎么才能把它复制到我想要的那一行?
Sub Major2_Paster()
Dim LastRow As Integer
Dim i As Integer
Dim erow As Integer
LastRow = Cells(Rows.count, 1).End(xlUp).Row
For i = 2 To LastRow
If Cells(i, 12) = “MLA” Then
range(Cells(i, 1), Cells(i, 21)).Select
Selection.Copy
Workbooks.Open Filename:="H:\Degrees List\Sorted_Workbooks\MLA Mar-17.xlsx"
erow = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
End Sub
答案 0 :(得分:2)
有几件事:
ActiveWorkbook
/ Sub Major2_Paster()
Dim LastRow As Integer, i As Integer, erow As Integer
Dim destinationWorkbook As Workbook
Dim sourceWorksheet As Worksheet, destinationWorksheet As Worksheet
Set destinationWorkbook = Workbooks.Open(Filename:="H:\Degrees List\Sorted_Workbooks\MLA Mar-17.xlsx")
Set sourceWorksheet = ThisWorkbook.Worksheets("SheetName")
Set destinationWorksheet = destinationWorkbook.Worksheets("SheetName")
With sourceWorksheet
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
For i = 2 To LastRow
If sourceWorksheet.Cells(i, 12).Value = “MLA” Then
With destinationWorksheet
erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
End With
destinationWorksheet.Cells(erow, 1).Resize(1, 21).Value = sourceWorksheet.Range(sourceWorksheet.Cells(i, 1), sourceWorksheet.Cells(i, 21)).Value
End If
Next i
destinationWorkbook.Close SaveChanges:=True
Application.CutCopyMode = False
End Sub
map[string]int