0

I am trying to cycle through a number of worksheets, then listbox controls within each worksheet and populate them with my code. I am using the code below:

Dim sh As Worksheet
Dim obj As OLEObject
Dim lst As MSForms.ListBox
Dim idx As Long

For idx = 1 To ThisWorkbook.Worksheets.Count
    Set sh = ThisWorkbook.Worksheets(idx)
    For Each obj In sh.OLEObjects
        If obj.progID = "Forms.ListBox.1" Then
            Set lst = obj
            If (lst.Name = "lst1") Then
                Call PopulateSimple(lst, "Table1")
            End If
        End If
    Next
Next idx

This seems to fail unfortunately when I set the listbox to the object. Any idea as to how I can achieve looping through all the listboxes in different worksheets and then populating them?

4
  • What reference(s) do you require for this? MSForms.ListBox gives me a compile error, "User defined type not recognized" Commented Apr 10, 2013 at 14:54
  • Disregard, I found a reference to MS Forms library. When I run this code, sh.OLEObjects is empty. How have you inserted the ListBox control? Commented Apr 10, 2013 at 14:55
  • 2
    In my opinion you need to change Dim lst as MSForms.ListBox into Dim lst as OLEObject and that is all... Commented Apr 10, 2013 at 14:57
  • + 1 @KazJaw: Yup that is the problem. However we don't need lst at all. Commented Apr 10, 2013 at 15:00

3 Answers 3

2

Try this

Sub Sample()
    Dim sh As Worksheet
    Dim obj As OLEObject
    Dim idx As Long

    For idx = 1 To ThisWorkbook.Worksheets.Count
        Set sh = ThisWorkbook.Worksheets(idx)
        For Each obj In sh.OLEObjects
            If TypeOf obj.Object Is MSForms.ListBox Then
                If (obj.Name = "lst1") Then
                    Call PopulateSimple(obj, "Table1")
                End If
            End If
        Next
    Next idx
End Sub

Explanation: You are getting that error because obj is declared as OLEObject and lst as MSForms.ListBox and hence a type mismatch.

Sign up to request clarification or add additional context in comments.

2 Comments

you beat me to it by about 3 seconds !
@Philip: Thanks Philip :) 3 Secs? 4 mins you mean? ;)
0

can't you use the obj in your call to PopulateSimplesince you know it is a ListBox:

Dim sh As Worksheet
Dim obj As OLEObject
Dim lst As MSForms.ListBox
Dim idx As Long

For idx = 1 To ThisWorkbook.Worksheets.Count
    Set sh = ThisWorkbook.Worksheets(idx)
    For Each obj In sh.OLEObjects
        If obj.progID = "Forms.ListBox.1" Then
            'Set lst = obj
            If (obj.Name = "lst1") Then
                Call PopulateSimple(obj, "Table1")
            End If
        End If
    Next
Next idx

Comments

0

simplest way (moved from comment):

In my opinion you need to change

Dim lst as MSForms.ListBox

into

Dim lst as OLEObject 

and that is all...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.