运行时错误91-在用户窗体中测试代码时

时间:2019-06-17 01:59:36

标签: excel vba variables userform

Office 360​​,64位,Excel。 子例程Private Sub Card_FindCardNumber_AfterUpdate()是一个textbox (Card_FindCardNumber),在我输入要查询的号码后,代码应查看它是否存在。

在测试代码时,无论它是存在还是不存在的数字都无所谓,我得到的错误消息是"Runtime Error 91", Object Variable or With Block variable not set

我是VBA的业余爱好者,无法弄清为什么特定代码行存在问题。

我改编了YouTube和他的网站上的Trevor Easton(在线PC学习)“ Excel VBA with Userform Vlookup”中的代码:

Ws01实际上是代表cnVisitorCard_Database工作表的电子表格的代码名称。

Vlookup指的是命名范围:“ LookupCardNo”,它具有一个偏移量功能,覆盖9列数据(从AC列(卡号)到名称范围AK的最后一列)

    Private Sub Card_FindCardNumber_AfterUpdate()
              'Define the Variables
              Dim ws01 As Worksheet
              Dim cnVisitorCard_Database As Worksheet
              Set ws01 = cnVisitorCard_Database

             'Check to see if value exists
             If WorksheetFunction.CountIf(ws01.Range("AC:AC"), Me.Card_FindCardNumber.Value) = 0 Then
                MsgBox "This is an incorrect ID"
                Me.Card_FindCardNumber.Value = ""
                Exit Sub
             End If

            'Lookup values based on first control
               With Me
                .Card_ExpiryDate = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 2, 0)
                .Card_Status = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 3, 0)
                .Card_ReturnDate = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 4, 0)
                .Card_Description = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 5, 0)
                .Card_TypeCode_Hidden = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 6, 0)
                .Card_ValidNo_ofDays_Hidden = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 7, 0)
                .Card_UpdatedInHW = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 8, 0)
                .Card_UpdatedInFF = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 9, 0)

                End With

     End Sub

我在行中收到错误91:

 If WorksheetFunction.CountIf(ws01.Range("AC:AC"), 
Me.Card_FindCardNumber.Value) = 0 Then

我当然希望它在数字存在的情况下将值返回到用户窗体的文本框中,但如果不存在,那么我会期望它用弹出消息告诉我该号码不存在。

如果能为您提供帮助,我将不胜感激:)当然,一旦清除,我将不知道是否还会有其他错误。

1 个答案:

答案 0 :(得分:1)

如果您已经有一个代号为cnVisitorCard_Database的工作表,则此变量声明将“隐藏”该工作表:

Dim cnVisitorCard_Database As Worksheet

因此您的局部变量的值为Nothing。因此,当您这样做时:

Set ws01 = cnVisitorCard_Database

你真的在写

Set ws01 = Nothing

这不会引发错误,但是会使下一行的Countif调用失败。

这应该可以工作(并且代码更简单/更短):

Private Sub Card_FindCardNumber_AfterUpdate()

    Dim rngSrch As Range, m 'as variant

    Set rngSearch = cnVisitorCard_Database.Range("LookupCardNo")

    m = Application.Match(CLng(Me.Card_FindCardNumber), rngSearch.Columns(1), 0)

    If IsError(m) Then
        'if no match then m is an error value
        MsgBox "This is an incorrect ID"
        Me.Card_FindCardNumber.Value = ""
    Else
        'if found match then m is the matching row number in rngSearch
        With rngSearch.Rows(m)
            Me.Card_ExpiryDate = .Cells(2).Value
            '...etc
            Me.Card_UpdatedInFF = .Cells(9).Value
        End With
    End If
End Sub