Excel VBA应用程序定义或对象定义的错误

我正在为这个VBA代码苦苦挣扎。 出于某种原因,我不断收到:“运行时错误”1004“:应用程序定义的或对象定义的错误”消息。 我唯一的想法是,为什么这可能是我有一个命令button绑定到一个特定的表单相关的macros – 如果这是错误,我该如何解决它? 否则,这里可能是什么错误?

导致错误的行是以MsgBox开始的行。

Sub Process() Sheets("Intermediate").Cells(2, 1).Select Dim contains As Range, lastRow As Long Do Until IsEmpty(ActiveCell) MsgBox Sheets("Document Library").Columns(1).Find(ActiveCell.Value) ActiveCell.Offset(1, 0).Select Loop End Sub 

谢谢!

除了使用Sid链接作为参考之外,值得注意的是, Find()的各种设置是持久的 – 也就是说,如果您以特定的方式(通过UI或使用VBA)使用Find,那么下次打电话除非另有说明,否则将使用相同的设置。

因此,每次使用Find时, 指定所关心的所有设置 (例如lookAtlookIn )总是一种很好的做法,否则您可能无法获得您期望的结果。

此外,您需要处理事件,您没有find正在search的值 – 在这种情况下查找返回Nothing

 Sub Process() Dim f as Range, c As Range, rngSearch as Range Set rngSearch = Sheets("Document Library").Columns(1) Set c = Sheets("Intermediate").Cells(2, 1) Do While Len(c.Value)>0 'specify exactly how you want Find() to operate.... Set f = rngSearch.Find(What:=c.Value, lookin:=xlValues, lookat:=xlWhole) If Not f Is Nothing Then debug.print c.value & " found at " & f.address() Else debug.print c.value & " not found!" End If Set c = c.Offset(1, 0) Loop End Sub 

Range.Find方法返回一个Range对象。 MsgBox函数需要输出stringexpression式。 所以试试这个:

 MsgBox Sheets("Document Library").Columns(1).Find(ActiveCell.Value).Value 

请注意,根据单元格中值的types,您可能还需要对string进行转换。