Excel VBA - Range.Find错误1004

时间:2017-06-20 09:19:41

标签: excel vba find range

下面我有一些代码:

Sub Priority()

ActiveSheet.Name = "Data"

Dim ws As Worksheet
Set ws = Sheets("Data")

Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Sheets.Add(After:= _
         ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws2.Name = "S"

Dim copyRange As Range
Dim firstAddress As String

Row = 2
Dim i As Integer
For i = 1 To 1500


    Set copyRange = ws.Range(Cells(i, 2), Cells(i, 2)).Find("pip", , , xlPart) 'The error pulls up at this line

        If Not copyRange Is Nothing Then

            Copied = ws.Range(Cells(i, 1), Cells(i, 17)).Value
            ws2.Activate
            ws2.Range(Cells(Row, 1), Cells(Row, 17)).Value = Copied

            Row = Row + 1

            ws.Activate

        End If

    Next i

End Sub

理论上应该从1-1500开始查看B列中的单元格并检查单元格中是否包含“pip”。如果是,则将该行复制并粘贴到不同的工作表。我知道IF语句是正确的,因为如果我要用其他条件(不能找到)替换“IF”行,它就可以了。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题是因为在线:

 Set copyRange = ws.Range(Cells(i, 2), Cells(i, 2)).Find("feas", , , xlPart)

应该是

Set copyRange = ws.Cells(i, 2).Find("feas", , , xlPart)

其他问题是我需要在“If Not”声明后激活工作表“ws”。