通过if条件获取数据并导出到另一个Excel的代码

时间:2019-07-16 13:53:34

标签: excel vba

我是编码的新手,在检查了一些条件并将其放入另一个Excel工作表后,编写了以下代码以从选定的单元格中获取特定数据。它抛出

  

需要消息对象的编译错误。

代码是:

Sub test()
    Dim LastRow As Integer, i As Integer, erow As Integer, a As Integer, b As 
    Integer

    LastRow = ActiveSheet.Range(“A” & Rows.Count).End(xlUp).Row

    For i = 3 To LastRow
        If Cells(i, 3) = "Grade" And Cells(i, 4) <> Null Then
            Set a = ActiveSheet.Cells(i, 3)
            Set b = ActiveSheet.Cells(i, 7)
            Union(a, b).Select

            Selection.Copy

            Workbooks.Open Filename:="C:\Users\413302\Desktop\Test.xlsx"

            Worksheets(“Sheet1”).Select
            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 个答案:

没有答案