根据标准将范围从一个工作表复制到另一个工作表

我想根据N列给出的标准将单元格范围复制到另一个工作表中。因此,对于每一行,都必须检查它是否满足列N中的条件。如果列N中的值为1 ,则应复制从第10行开始,从Range(Cells(j, 1), Cells(j, 8))Range(Cells(j, 1), Cells(j, 8))到另一个工作表。如果N = 0列中的值跳过该行并检查下一行。 所以它不会复制那一行。

也许我的错误代码可以比我更好地解释它:

 Sub TCoutput() Dim i As New Worksheet Dim e As New Worksheet Set i = ActiveWorkbook.Worksheet.Item(3) Set e = ActiveWorkbook.Worksheets.Item(4) Dim d Dim j d = 10 j = 3 Do Until IsEmpty(i.Range("N" & j)) If i.Range("N" & j) = "1" Then d = d + 1 e.Range(Cells(d, 1), Cells(d, 8)) = i.Range(Cells(j, 1), Cells(j,8)) End If j = j + 1 Loop End Sub 

尝试这个。 我添加.value和d = d + 1

  Sub TCoutput() Dim i As New Worksheet Dim e As New Worksheet Set i = ActiveWorkbook.Worksheets.Item(1) Set e = ActiveWorkbook.Worksheets.Item(2) Dim d Dim j d = 10 j = 3 Do Until IsEmpty(i.Range("N" & j)) If i.Range("N" & j) = "1" Then e.Range(e.Cells(d, 1), e.Cells(d, 8)).Value = i.Range(i.Cells(j, 1), i.Cells(j, 8)).Value d = d + 1 End If j = j + 1 Loop End Sub 

在使用多个电子表格时,您需要小心,并确保所有的.Range.Cells引用都包含您想要的工作表。 首先要把你的If语句replace成这个:

 If i.Range("N" & j) = "1" Then e.Range(e.Cells(d, 1), e.Cells(d, 8)) = i.Range(i.Cells(j, 1), i.Cells(j,8)) End If 

或者,你可以使用With (我个人比较喜欢):

 With i If .Range("N" & j) = "1" Then e.Range(e.Cells(d,1),e.Cells(d,8)) = .Range(.Cells(j,1),.Cells(j,8)) End If End with 

如果没有显式引用工作表, Cells()Range()将推迟到ActiveSheet任何一个。