我正在尝试创建VBA代码,该代码将从一个工作簿(Test Report.xlsx)复制行,然后将复制的行粘贴到另一工作簿(Test Final Report.xlsx)(如果单击命令按钮时它符合特定条件)。如果表第二列中的单元格包含critera 3'5,则复制和粘贴的条件是a,复制该行,然后将其粘贴到另一个工作簿中。我尝试了以下操作,但是当我运行时,它只是选择End Sub之前的代码行中指定的单元格。我需要协助,请在下方查看代码,这些代码会让我失望。我在做错什么,应该怎么做才对呢?
Private Sub CommandButton1_Click()
a = Worksheets("Mar'19").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Mar'19").Cells(i, 2).Value = "3'5" Then
Worksheets("Mar'19").Rows(i).Copy
Worksheets("3.5").Activate
b = Worksheets("3.5").Cells(Rows.Count, b).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Mar'19").Cells(1, 1).Select
End Sub
答案 0 :(得分:0)
这行吗?
Private Sub test()
a = Worksheets("Mar'19").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Mar'19").Cells(i, 2).Value = "3'5" Then
Worksheets("Mar'19").Rows(i).Copy
Worksheets("3.5").Cells(ThisWorkbook.Sheets("3.5").Cells(1048576, 1).End(xlUp).row + 1, 1).Paste
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Mar'19").Cells(1, 1).Select
End Sub