PDA

View Full Version : VBA Error 91



coliervile
09-07-2016, 04:52 AM
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


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

nilem
09-07-2016, 05:32 AM
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
....

coliervile
09-07-2016, 06:18 AM
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

nilem
09-07-2016, 06:54 AM
could you attach a file, in which you get an error

Paul_Hossler
09-07-2016, 08:45 AM
Put a break point on the first line and step through with a name that throws an error.

Identify the offending line