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.
You set
startCellequal to 0.You then set
topCount=startCellYou don’t do anything else with
topCountbefore starting the loop.Therefore, this:
Evaluates to
There is no such thing as cell A0. Try starting
startCellat 1.