Error code is 91 on Find

*爱你&永不变心* 提交于 2021-02-16 19:59:40

问题


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")

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

Error code 5: Invalid procedure call or argument.


回答1:


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

Pertinent to your case search area ("B3:B54") the rowIdx can be declared As Integer; for extended area you may use Long.

Also, as mentioned in comments thread, you may declare: Dim c As Range.

Hope this may help.



来源:https://stackoverflow.com/questions/35609913/error-code-is-91-on-find

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!