简体   繁体   English

如果满足条件,复制并粘贴到另一个工作簿

[英]Copy and paste to another workbook if criteria is met

I am trying to create VBA code which copies rows from one workbook (Test Report.xlsx) and then pastes that copied row into another workbook(Test Final Report.xlsx), if it meets specific criteria when the commandbutton is clicked.我正在尝试创建 VBA 代码,该代码从一个工作簿(Test Report.xlsx)复制行,然后将该复制的行粘贴到另一个工作簿(Test Final Report.xlsx)中,如果它在单击命令按钮时满足特定条件。 The criteria to copy and paste is a if a cell in the second column in the table contains the critera 3'5, copy that row and then paste it into another workbook.复制和粘贴的条件是,如果表中第二列中的单元格包含条件 3'5,则复制该行,然后将其粘贴到另一个工作簿中。 I've tried the following but when I go to run, it just selects cells specified in the line of code before End Sub.我尝试了以下方法,但是当我运行 go 时,它只选择在 End Sub 之前的代码行中指定的单元格。 I need assistance with this, please see below the code which keeps failing me.我需要这方面的帮助,请参阅下面一直让我失望的代码。 What am I doing wrong and what should I do to make it right?我做错了什么,我应该怎么做才能使它正确?

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

Does this work?这行得通吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM