查找时错误代码为91 [英] Error code is 91 on Find
问题描述
这似乎是一个简单的问题,但是我对VBA还是很陌生,我不确定为什么会收到错误消息.
This may seem like a simple question but Im very new to VBA and Im not sure why I'm receiving the error.
Dim c As String
c = Sheet2.Range("B3:B54").Find("NLwk01")
错误代码为91:对象变量或未设置带块变量.
Error code is 91: Object variable or With block variable not set.
我以为我应该使用单元格而不是范围,但这给
I thought I should've maybe used cells instead of range, but that gives another error with
错误代码5:无效的过程调用或参数.
Error code 5: Invalid procedure call or argument.
推荐答案
如注释线程中所述,Excel VBA Find()
函数返回 Range
对象.因此,与您的特定示例有关,可以将其编码为以下示例代码片段:
As it was mentioned in comment thread, Excel VBA Find()
function returns the Range
object. Therefore, pertinent to you particular example, it could be coded as in the following sample snippet:
Sub FindRowIndex()
Dim c
Dim rowIdx As Integer
Dim cellValue As String
'return Range object if found
Set c = Sheet2.Range("B3:B54").Find("NLwk01")
If Not c Is Nothing Then
'return the row index (shown as an example)
rowIdx = c.Row
'return the same string used as search criterion "NLwk01"
cellValue = c.Value
End If
End Sub
与您的案例搜索区域("B3:B54"
)相关,可以将 rowIdx
声明为 Integer
;对于扩展区域,您可以使用 Long
.
Pertinent to your case search area ("B3:B54"
) the rowIdx
can be declared As Integer
; for extended area you may use Long
.
此外,如注释线程中所述,您可以声明: Dim c As Range
.
Also, as mentioned in comments thread, you may declare: Dim c As Range
.
希望这会有所帮助.
这篇关于查找时错误代码为91的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!