Error Message - Run-time error ‘91’: Object Variable or With Block variable not set

abittolo

New Member
Joined
Sep 12, 2018
Messages
10
Hello everybody,
I'm building a quite complex Excel model - complex at least for me.

Objects involved are basically 3:
- Worksheet containing data;
- Userform1 (UFSchedaNavigazione) where I have the list of data contained in Worksheet;
- Userform2 (UFImmagini) where I have a set of pictures whose names contain the row of the related data in Worksheet;
- Userform 3 (UFVisualizzaGiocatore) where I want to display after selection in UFSchedaNavigazione data contained in Worksheet plus image from Userform (UFImmagini).

But, when I doubleclick on the item in listbox of UFSchedaNavigazione, I receive the message "Run-time error ‘91’: Object Variable or With Block variable not set".

Here below the code contained in Userform UFScheda Navigazione:


Private Sub LstBoxRicercaGiocatore_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

For i = 0 To LstBoxRicercaGiocatore.ListCount - 1
If LstBoxRicercaGiocatore.Selected(i) = True Then
'set the listbox column
ID = LstBoxRicercaGiocatore.List(i, 0)
IDVisualizza = ID
End If
Next i
UFVisualizzaGiocatore.Show
End Sub


While, here below the code contained in UFVisualizzaGiocatore:

Private Sub UserForm_Initialize()
Dim rngID As Range
Dim X As String
Dim objPic As IPictureDisp
Dim shp As Shape
Dim pic As Shape
Dim Row As Long
Dim ImgRiga As String
Dim ImgCarica As Image

Me.Image1.BackColor = RGB(4, 34, 46)
Me.LblTitolo.BackColor = RGB(4, 34, 46)

Me.TxtVID = IDVisualizza

Worksheets(2).Activate
LastRow = Worksheets(2).Range("A" & Rows.Count).End(xlUp).Row

Set rngNome = Worksheets(2).Range("A2:A" & LastRow)
For Each rngID In rngNome
If rngID.Value = IDVisualizza Then
Row = rngID.Row
Me.TxtVNome.Value = rngID.Offset(, 1).Value
Me.TxtVCognome.Value = rngID.Offset(, 2).Value
Me.TxtVAnnoNascita.Value = rngID.Offset(, 3).Value
End If
ImgRiga = "Img" & Row & ".Picture"
ImgCarica = "UFImmagini." & ImgRiga
UFImmagini.Hide
Me.ImgVGiocatore.Picture = ImgCarica
Next rngID

End Sub

As said, when I doubleclick on item of the Listbox contained in UFSchedaNavigazione, I get the error mesage. Any clue how I can sort this out?

Thanks,
A

 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Where in the code do you get the error?
 
Upvote 0
Hi Norie, thanks for your attention.
When I doubleclick on the item in the listbox I get the message.
Then, if I click on “Debug”, Excel highlights me the line “UFVisualizzaGiocatore.Show”.

A
 
Upvote 0
In the VBE click on Tools > Options > General > Select "Break in class module" > OK.
The run the code again & what line gets highlighted?
 
Upvote 0
Thanks Fluff.
Done it. Now the highlighted line is:

Code:
ImgCarica = "UFImmagini." & ImgRiga

I then checked a little bit in the code and found out that Row variable at the beginning of the "If" statement is not properly valued, and becomes equal to "0" in the line above:

Code:
ImgRiga = "Img" & Row & ".Picture"

while, for example, it was supposed to be 47 in this case.

Any suggestion?

A
 
Upvote 0
Firstly change the name of the variable "Row" to something else. It's best to avoid using VBA keywords as variables, as it can cause problems.
If that does not solve the problem report back
 
Upvote 0
Hi Fluff,
I've done what you suggested and I reworked a little bit the code to get the right value of the row.
Now this is how the code is:

Code:
Me.TxtVID = IDVisualizza

Worksheets(2).Activate
rngID = 2
X = Worksheets(2).Cells(rngID, 1).Value


Do While Not X = IDVisualizza
rngID = rngID + 1
X = Worksheets(2).Cells(rngID, 1).Value
Loop
UltimaRiga = rngID


Me.TxtVID = Worksheets(2).Cells(rngID, 1).Value


ImgRiga = "Img" & rngID & ".Picture"
ImgCarica = "UFImmagini." & ImgRiga
Me.ImgVGiocatore.Picture = ImgCarica

Now, ImgRiga is properly filled with the right value ("Img47.Picture"), but ImgCarica is not valued (it is declared as Object). As a result the following line it is not executed.

Can you give an additional suggestion on how I could get it done?

Thanks,
A
 
Upvote 0
I've never worked with pictures, so am unable to help any further.
 
Upvote 0
'Img47.Picture' is only a string, you can't set the Picture property of an image control, e.g. ImgVGiocatore, with that.
 
Upvote 0
Dear Norie,
first of all thanks. I don't want to be disrespectful but I see by myself that the method I'm using unfortunately is not working. And this is quite frustrating. That's why I have posted this thread to you "Gurus" of Excel/VBA to get your support.

Please consider that same pictures are reported into the spreadsheets with speaking-codes (e.g. Img47), but again, I tried but I'm not able to load them into Image box on the userform at runtime.

Can you support? If you want I can also share the workbook here.

Thanks,
Andre
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top