2012-02-08 71 views
0

我的问题是关于Excel中VBA中的运行时错误91。我做了一些搜索无济于事。我的代码如下。我注意到导致错误的部分。为什么会发生这种情况,我如何解决它并继续前进?查找函数中的VBA运行时错误91

Sub RemoveFooterRows(theFile) 
    Dim found As Range 
    Dim aggregateRow 

    ''Error is from section below 
    found = isItRow = Workbooks(theFile).Worksheets(1).Columns(1).Find _ 
     ("Summary", Range("A1"), xlValues, xlPart, xlByRows, xlNext, False, , False) 
    ''Error is from section above 

    MsgBox ("val is " & found.Row) 

End Sub 

回答

3
Sub RemoveFooterRows(theFile) 

    Dim found As Range 

    Set found = Workbooks(theFile).Worksheets(1).Columns(1).Find _ 
     ("Summary", Range("A1"), xlValues, xlPart, xlByRows, xlNext, False, , False) 
    MsgBox ("val is " & found.Row) 

End Sub 

您需要的 “设置” 关键字来分配值。

而且不知道你想要什么“= isItRow =”做,但你应该这样做在两个语句,而不是试图将它们堆叠这样。

此外,还未使用aggregateRow并未分配类型。

+3

也值得添加一个检查'如果没有发现什么都没有然后...'在未找到值的情况下。 – 2012-02-08 19:35:20

+0

好的,那是有效的,我没有意识到我有双重任务。但是,当我添加一个.Row的时候,我怎么得到这一行,我得到了同样的错误。如何将找到的行分配给变量? – Brian 2012-02-08 19:40:34

+0

添加.row到什么?它已经在MsgBox行中,并且对我来说还不错。 – Jesse 2012-02-08 20:50:03

0

您使用SET来查找单元格并将其放入对象中。但是如果你只是在排这一行,你可以让SET出来。这是我如何写测试到目前为止:

Dim Rw As Long 

On Error Resume Next 
Rw = Workbooks(theFile).Worksheets(1).Columns(1).Find _ 
    ("Summary", LookIn:=xlValues, LookAt:=xlPart).Row 

If Rw > 0 Then 
    MsgBox "The row is " & Rw 
Else 
    MsgBox "Not found" 
End If 
+0

您的支票和'If Not found Is Nothing Then'之间的区别是什么 – Brian 2012-02-08 20:14:22

+0

使用您的代码并将其注释掉在错误继续之后,我得到相同的错误。我使用了这个发现,因为我写了它。我知道该值在列A中,为什么错误存在? – Brian 2012-02-08 20:18:39

+0

'On Error Resume Next'是因为如果'.Find'找不到任何东西,那么'.Row'会产生一个错误。 – mischab1 2012-02-09 00:50:52