1
votes

I am making a macro that will search though a list and find all the entries in a column that has spectraseven at the first. This will be to copy these records to sheet for each entry.

With the code so far it fails with:

Run-time error '1004': Application-defined or object-defined error.

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

The arrow points to the error.

1

1 Answers

1
votes

You set startCell equal to 0.

You then set topCount = startCell

You don't do anything else with topCount before starting the loop.

Therefore, this:

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

Evaluates to

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

There is no such thing as cell A0. Try starting startCell at 1.