使用字符串变量来设置VBA中的对象变量? (Excel 2013) [英] Use string variable to set object variable in VBA? (Excel 2013)
问题描述
我在页面上有一些ActiveX控件/按钮,我想修改按钮的几个参数(在循环函数中)。
我很好,编写循环函数来实现这一点,但是找不到一种使用字符串变量来引用对象的方法。我已经设置了一个对象变量(如下所示)和一个用于更改对象变量引用的字符串变量 - 但找不到一种方法来使其变得正常工作。
这是不工作的代码:
Private Sub TrialCode_Click ()
Dim ButtonObj As Object
Dim ButtonCaption As String
Dim ButtonString As String
ButtonString =CommandButton1
设置ButtonObj = ButtonString
ButtonCaption =Something
ButtonObj.Caption = ButtonCaption要更改的参数类型的示例
End Sub
Set ButtonObj = ButtonString
是失败的命令,报告类型不匹配错误。
我正在Excel 2013工作。
<我真的希望有一些方法可以做到这一点。任何帮助将非常感谢!!!
CommandButton属于 OLEObject
尝试
ButtonString =CommandButton1
设置ButtonObj = ActiveSheet.OLEObjects(ButtonString)
ButtonCaption =Something
ButtonObj.Object.Caption = ButtonCaption要更改的参数类型的示例
请注意,某些属性直接出现在 ButtonObj
下,其他如 cuBmL.pngalt =在此输入图像说明>
I have a number of ActiveX controls/buttons on a page, and I would like to modify several of the parameters of the buttons (in a loop function).
I am fine with writing the loop function to achieve this, but cannot find a way to refer to the object using a string variable. I have set up an object variable (as per below), and a string variable to be used to change the reference for the object variable - but can't find a way to get it to work.
This is the code that does NOT work:
Private Sub TrialCode_Click()
Dim ButtonObj As Object
Dim ButtonCaption As String
Dim ButtonString As String
ButtonString = "CommandButton1"
Set ButtonObj = ButtonString
ButtonCaption = "Something"
ButtonObj.Caption = ButtonCaption 'example of the kind of parameters I want to change
End Sub
The Set ButtonObj = ButtonString
is the command that fails, reporting a Type Mismatch error.
I'm working in Excel 2013.
I really hope there is some way to do this. Any help will be really appreciated!!!
The CommandButton belongs to an OLEObject
try
ButtonString = "CommandButton1"
Set ButtonObj = ActiveSheet.OLEObjects(ButtonString)
ButtonCaption = "Something"
ButtonObj.Object.Caption = ButtonCaption 'example of the kind of parameters I want to change
Note that some properties occur directly under ButtonObj
, others such as Caption sit below Object
这篇关于使用字符串变量来设置VBA中的对象变量? (Excel 2013)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!