gpt4 book ai didi

excel - 使用字符串变量在 VBA 中设置对象变量? (Excel 2013)

转载 作者:行者123 更新时间:2023-12-04 20:06:38 26 4
gpt4 key购买 nike

我在一个页面上有许多 ActiveX 控件/按钮,我想修改按钮的几个参数(在循环函数中)。

我可以编写循环函数来实现这一点,但找不到使用字符串变量引用对象的方法。我已经设置了一个对象变量(如下所示)和一个用于更改对象变量引用的字符串变量 - 但找不到让它工作的方法。

这是不起作用的代码:

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
Set ButtonObj = ButtonString是失败的命令,报告类型不匹配错误。

我在 Excel 2013 中工作。

我真的希望有一些方法可以做到这一点。任何帮助将不胜感激!!!

最佳答案

命令按钮属于 OLEObject
尝试

ButtonString = "CommandButton1"
Set ButtonObj = ActiveSheet.OLEObjects(ButtonString)

ButtonCaption = "Something"
ButtonObj.Object.Caption = ButtonCaption 'example of the kind of parameters I want to change

请注意,某些属性直接出现在 ButtonObj 下。 , 其他如 Caption 位于 Object 下面

enter image description here

关于excel - 使用字符串变量在 VBA 中设置对象变量? (Excel 2013),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27614692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com