[英]Iterate Over Static Array of Objects
I want to iterate over several ComboBox objects in the same Worksheet sht
:我想在同一个 Worksheet
sht
中迭代几个 ComboBox 对象:
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 ...
)运行时错误 424:需要对象(在
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.
运行时错误 91:对象变量或未设置块变量。 (raised at
obj = ...
)(在
obj = ...
提出)
What is the correct way?正确的方法是什么?
And why don't these approaches work as I would expect them to?为什么这些方法不能像我期望的那样工作?
Prepending Set
as suggested by @KostasK.按照@KostasK 的建议预先
Set
。 to the assignment works:到作业:
Dim s as Variant
Dim obj As ComboBox
Dim sht as Worksheet
...
For Each s In Array("ComboBox1", "ComboBox2")
Set obj = CallByName(sht, s, VbGet)
Next s
The ComboBox is part of Worksheet.OLEObjects
. ComboBox 是
Worksheet.OLEObjects
的一部分。 In this case obj
must be declared as type OLEObject
to work (as long as Option Explicit
is set)在这种情况下,
obj
必须声明为OLEObject
类型才能工作(只要设置了Option Explicit
)
Dim s as Variant
Dim obj As OLEObject
Dim sht as Worksheet
...
For Each s In Array("ComboBox1", "ComboBox2")
Set obj = sht.OLEObjects(s)
Next s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.