I want to iterate over several ComboBox objects in the same Worksheet sht:
Dim obj As ComboBox
Dim sht as Worksheet
...
For Each obj In Array(sht.ComboBox1, sht.ComboBox2)
Next obj
Runtime error 424: object required (raised at
For Each ...)
I also tried to address the objects by iterating over the names:
Dim s as Variant
Dim obj As ComboBox
Dim sht as Worksheet
...
For Each s In Array("ComboBox1", "ComboBox2")
obj = CallByName(sht, s, VbGet)
Next s
Runtime error 91: Object variable or With block variable not set. (raised at
obj = ...)
What is the correct way?
And why don't these approaches work as I would expect them to?
Approach 1
Prepending
Setas suggested by @KostasK. to the assignment works:Approach 2
The ComboBox is part of
Worksheet.OLEObjects. In this caseobjmust be declared as typeOLEObjectto work (as long asOption Explicitis set)