Consulting

Results 1 to 5 of 5

Thread: VBA Error 91

  1. #1
    VBAX Expert
    Joined
    May 2006
    Location
    Oklahoma City, OK
    Posts
    532
    Location

    VBA Error 91

    Good day to everyone-

    I have a database that uses a userform to show information on my student testing scores. When I double click on some of the students names in the listbox named "lstLookup" I receive this error message ... "Runtime Error 91: Object variable or with block variable not set" (see code below). I only get the error on about half of the students. Can someone please help and explain why this happens and how to resolve it?

    Thank you Charlie

    HTML Code:
    Private Sub lstLookup_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        'declare the variables
        Dim cIDNumber As String
        Dim I As Integer
        Dim findvalue
        'error block
        On Error GoTo errHandler:
        'get the select value from the listbox
        For I = 0 To lstLookup.ListCount - 1
            If lstLookup.Selected(I) = True Then
                cIDNumber = lstLookup.List(I, 1)
            End If
        Next I
        'find the Id number
        Set findvalue = Sheet2.Range("F:F").Find(What:=cIDNumber, LookIn:=xlValues).Offset(0, -3)
        'add the database values to the userform
        cNum = 41
        For X = 1 To cNum
            Me.Controls("Reg" & X).Value = findvalue
            Set findvalue = findvalue.Offset(0, 1)
        Next
        'disable adding
        Me.cmdAdd.Enabled = False
        Me.cmdEdit.Enabled = True
        'error block
        On Error GoTo 0
        Exit Sub
    errHandler::
        MsgBox "An Error has Occurred  " & vbCrLf & "The error number is:  " _
               & Err.Number & vbCrLf & Err.Description & vbCrLf & _
               "Please notify the administrator"
    End Sub
    Best regards,

    Charlie

    I need all the I can get....

  2. #2
    VBAX Regular
    Joined
    Nov 2011
    Location
    Ufa
    Posts
    75
    Location
    not sure, but maybe so
    ....
    'find the Id number
    Set findvalue = Sheet2.Range("F:F").Find(What:=cIDNumber, LookIn:=xlValues).Offset(0, -3)
    If findvalue Is Nothing Then MsgBox cIDNumber & " not found": Exit Sub' this line added
    'add the database values to the userform
    ....

  3. #3
    VBAX Expert
    Joined
    May 2006
    Location
    Oklahoma City, OK
    Posts
    532
    Location
    Thank you Nilem for looking at my question. I inserted your code suggestion, but I still get Error 91 on some of the students.

    Charlie
    Best regards,

    Charlie

    I need all the I can get....

  4. #4
    VBAX Regular
    Joined
    Nov 2011
    Location
    Ufa
    Posts
    75
    Location
    could you attach a file, in which you get an error

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Put a break point on the first line and step through with a name that throws an error.

    Identify the offending line
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •