尝试在工作表中查找匹配项时发生运行时错误'91'VBA

问题描述

我正在尝试在范围中查找特定的字符串。运行时错误'91'出现。我知道发生这种情况是因为Find函数找不到任何巧合。不过,我不确定为什么会这样。

目前,我在用户窗体中有一个列表框。当我在列表框中选择一个选项时,此类选择的字符串将显示用户窗体文本框中(txt_selected)。在工作表上,我具有与列表框上显示的完全相同的列表。我想在工作表中找到与所选元素一致的行。

我有一个if语句,当找到匹配项时(countif> 0)触发该if语句,显示一个带有消息“ Match found”的MessageBox。但是,当我尝试找到匹配项的行号时,我得到了错误

我的代码如下:

Private Sub lstbx_diarias_Click()

For n = 0 To (UserForm1.lstbx_diarias.ListCount - 1)
    UserForm1.txt_selected.Value = UserForm1.lstbx_diarias.Text
Next n

End Sub


Sub llenar_checklist()

Dim result_row As Long
Dim actividad As String
Dim look_range as Range

''' Actividades Diarias
If UserForm1.chbx_diarias.Value = True Then

    Set look_range = Sheets("Checklist").Range("C9:C23")

    actividad = UserForm1.txt_selected.Value

    If Application.WorksheetFunction.CountIf(look_range,actividad) > 0 Then

        MsgBox "Match found"
        result = look_range.Find(what:=actividad,LookIn:=xlValues,LookAt:=xlWhole)
        result_row = result.Row

    End If
    MsgBox "The row is " & result_row

End If

End Sub

根据VBA,错误自定义结果的行。为什么会发生这种情况的任何想法?

非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)