运行时错误'1004':应用程序定义的错误或对象定义的错误

时间:2012-08-22 16:14:44

标签: excel vba excel-vba

我正在创建一个宏,它将搜索列表并查找第一个具有spectraseven的列中的所有条目。这将是为每个条目将这些记录复制到工作表。

到目前为止,代码失败了:

  

运行时错误'1004':应用程序定义的错误或对象定义的错误。

Sub testWild()
startCell = 0
Dim FoundCell As Range
Dim LastCell As Range
Dim FirstAddr As String
cellRange = "A1:A20"
topCount = startCell
With Range("A1:A20")
Set LastCell = .Cells(.Cells.Count)
End With
Dim findString As String 
findString = "spectraseven*"
 Set FoundCell = Range(cellRange).Find(what:=findString, after:=LastCell)



If Not FoundCell Is Nothing Then
FirstAddr = FoundCell.Address
End If
Do Until FoundCell Is Nothing
    Debug.Print FoundCell.Address
Set FoundCell = Range(cellRange).FindNext(after:=FoundCell)

Count = FoundCell.Row
    Set FoundCell = Range(cellRange).FindNext(after:=FoundCell)

 --->   Sheets(1).Range("a" & topCount) = Sheets(1).Range("e" & Count)
    topCount = topCount + 1



If FoundCell.Address = FirstAddr Then
    Exit Do
End If
Loop
End Sub

箭头指向错误。

1 个答案:

答案 0 :(得分:1)

您将startCell设置为0。

然后设置topCount = startCell

在开始循环之前,您不会对topCount执行任何其他操作。

因此,这个:

Sheets(1).Range("a" & topCount) = Sheets(1).Range("e" & Count)

评估到

Sheets(1).Range("a0") = Sheets(1).Range("e" & FoundCell.Row)

没有单元格A0这样的东西。尝试从1开始startCell