从一张纸复印到另一张时发生运行时错误

我有下面列出的代码,我想复制包含在if语句中的规则的每一行,并将其粘贴到第二个工作表中。 这是我的:

Sub Button1_Click() Dim i As Long, j As Long Dim mainworkBook As Workbook Set mainworkBook = ActiveWorkbook Lastrow = Cells(Rows.Count, "Y").End(xlUp).Row j = 1 For i = 5 To Lastrow If (IsEmpty(Range(Cells(i, 2), Cells(i, 2)))) Then If IsNumeric(Range(Cells(i, 25), Cells(i, 25))) Then Range(Cells(i, 1), Cells(i, 1)).Value = "T" Range(Cells(i, 2), Cells(i, 24)).Value = Range(Cells(i - 2, 2), Cells(i - 2, 24)).Value Range(Cells(i, 26), Cells(i, 26)).Value = Range(Cells(i - 2, 26), Cells(i - 2, 26)).Value Range(Cells(i, 28), Cells(i, 28)).Value = Range(Cells(i - 2, 28), Cells(i - 2, 28)).Value Range(Cells(i, 30), Cells(i, 36)).Value = Range(Cells(i - 2, 30), Cells(i - 2, 36)).Value Range(Cells(i, 38), Cells(i, 39)).Value = Range(Cells(i - 2, 38), Cells(i - 2, 39)).Value mainworkBook.Worksheets("a").Rows(i).EntireRow.Copy mainworkBook.Worksheets(“b”).Range(Cells(j,1),Cells(j,1).Select mainworkBook.Sheets(“b”).Paste j = j + 1 End If End If Next i End Sub 

这是给我一个错误的“mainworkBook.Worksheets(”b“)。范围(单元格(j,1),单元格(j,1)。select”行。

你没有closures括号。

  mainworkBook.Worksheets("b").Range(Cells(j, 1), Cells(j, 1)).Select 

您不能select不活动的工作表上的单元格,也应该使用工作表限定单元格调用。 碰巧它只有一个单元,所以你不需要这个语法,并且直接复制起来更容易:

 mainworkBook.Worksheets("a").Rows(i).EntireRow.Copy Destination:=mainworkBook.Worksheets(“b”).Cells(j,1)