满足条件后复制一系列单元格

时间:2015-05-09 01:46:55

标签: vba excel-vba excel

在满足以下条件后,我需要复制一系列单元格:

每当sheet1的Cells(15,15)等于cell(14,j)。 Firstcell将等于单元格(14,j)。第二单元的概念相同。

之后,我将需要在Firstcell和Secondcell之间复制细胞。这是我开始看到运行时错误91。

假设我已经定义了lastCol。我的代码:

Dim firstcell as range, secondcell as range

If Sheets("Sheet1").Cells(15, 10) <> "" And Sheets("Sheet1").Cells(15, 15) <> "" Then

    For j = 10 To lastCol

        If Sheets("Sheet1").Cells(15, 15).Text = Sheets("Sheet1").Cells(14, j).Text Then

            firstcell = Sheets("Sheet1").Cells(15, j)

        End If

        If Sheets("Sheet1").Cells(15, 13).Text = Sheets("Sheet1").Cells(14, j).Text Then

            secondcell = Sheets("Sheet1").Cells(15, j)

        End If

    Range(firstcell, secondcell ).copy

    Next j

End If

1 个答案:

答案 0 :(得分:2)

通过评论确定答案:

Dim firstcell as range, secondcell as range

If Sheets("Sheet1").Cells(15, 10) <> "" And Sheets("Sheet1").Cells(15, 15) <> "" Then
    For j = 10 To lastCol
        If Sheets("Sheet1").Cells(15, 15).Text = Sheets("Sheet1").Cells(14, j).Text Then
            set firstcell = Sheets("Sheet1").Cells(15, j)
        End If

        If Sheets("Sheet1").Cells(15, 13).Text = Sheets("Sheet1").Cells(14, j).Text Then
            set secondcell = Sheets("Sheet1").Cells(15, j)
        End If
    Next j
    Range(firstcell, secondcell ).copy
End If