Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a function which a part shown below. The problem is that I get a Run-time error 91 (object variable or with variable not set) and the debug highlights the last line of the code ( Set pFeatureClass = pFeatureLayer.FeatureClass) shown below while the layer file exist on the table of contents. The strange thing is that when I use this function by giving directly the arguments as shown in the example with asterisks below it works fine but when the function is used within a loop in a procedure it presents that error 91.

Could you help me please?

Public Function GetAveX(BlockID As String, Solindex1 As String, Solindex2 As String _
, MPindex1 As String, MPindex2 As String, ParcelID As Long) As Double

'Get the current map document
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

'Get the active map (data frame)
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap

' Get the proper layer 
Dim pLayers As IEnumLayer
Set pLayers = pMap.Layers

Dim pLayer As ILayer
Set pLayer = pLayers.Next
Do Until pLayer Is Nothing
If pLayer.Name = "VectorB" & BlockID & "MP" & "_" & Solindex1 & "_" & MPindex1 Then
    Exit Do
End If
Set pLayer = pLayers.Next

'Get the feature class
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = pLayer

Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureLayer.FeatureClass

'rest of the code

'**********An example that works fine
Dim i As Double
i = GetAveX(25, 27, 36, 19, 20, 45)
MsgBox i
share|improve this question
Does the Do Until loop really succeed in finding the correct layer when it fails? Add some code to find out if you really find the correct layer before Exit do or test if pLayer is not nothing before accessing pFaeturelayer.featureclass – Stefan Jan 10 '12 at 10:19
Agree with @Stefan. Add the following line after immediately after the loop: if pLayer is nothing then exit function – Jakub Jan 10 '12 at 13:14
Look at the logic: you loop until (potentially) pLayer is Nothing, then drop out of the loop. Then you Set pFeatureLayer = pLayer, which is still Nothing. Finally, you request the .FeatureClass of pFeatureLayer, which--guess what!--is still Nothing. Making a request of Nothing is an error. This behavior must have occurred because the conditional statement inside the loop was never true. – whuber Jan 10 '12 at 14:52
Thanks for the comments. I have to note that the player inside the loop is indeed nothing and the question is why this happens while the shapefile with that name exist in the TOC. Why it does not get it and the pFeatureclass is nothing? Why the conditional statement is not tru while it should be true? – Demetris Jan 10 '12 at 15:43
Please, also note that now, when I run the code line by line while the player.Name within the loop is the same with the filename, the 'Exit Do' does not work, hence the looping is continuing until finsih thus pFeatureclass is nothing. Any, idea please? – Demetris Jan 11 '12 at 7:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.