vba 运行时错误“1004”:应用程序定义或对象定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12077292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:26:36  来源:igfitidea点击:

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

excelvbaexcel-vba

提问by monkthemighty

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.

运行时错误“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

The arrow points to the error.

箭头指向错误。

采纳答案by LittleBobbyTables - Au Revtheitroad

You set startCellequal to 0.

您设置为startCell等于 0。

You then set topCount= startCell

然后你设置topCount=startCell

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

topCount在开始循环之前你不做任何其他事情。

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 startCellat 1.

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