在VBA中,是否可以获取存储为变量的工作表上按钮的值?

huangapple go评论88阅读模式
英文:

In VBA is it possible to get the value of a button on a worksheet which is stored as a variable?

问题

In VBA中是否有一种方式可以引用作为变量保存的工作表上的按钮?除非我直接引用工作表,否则似乎会引发“找不到方法或数据成员”的编译错误。例如:

Sub Test_Parent
    Call Test_Child(Temp_WS:=Sheet1)
End Sub

Sub Test_Child(ByVal Temp_WS as Worksheet)
    Debug.Print Temp_WS.Example_Button.Value
End Sub

上述代码在debug.print行触发错误。然而,以下代码可以正常运行:

Sub Test2
    Debug.Print Sheet1.Example_Button.Value
End Sub

我的直觉是,这是因为在第二种情况下,编译器查看直接引用(例如Sheet1)并且可以看到按钮作为成员存在,而在第一种情况下,它查看变量并将其视为通用的工作表类型(而不是Sheet1),因此不认为会有这个成员存在?或者我完全错了吗?

不管怎样,是否有解决方法?(或者我是否做了一些愚蠢的事情?)如果没有,那也不是世界末日,但通常我喜欢将对工作表的硬编码引用的数量降到最低,所以如果这是一种解决方法,那就太好了。

英文:

In VBA is there a way to refer to a button on a worksheet that's held as a variable? Unless I refer to the sheet directly it seems to throw a compile error for "Method or data member not found". So for example:

Sub Test_Parent
    Call Test_Child(Temp_WS:=Sheet1)
End Sub

Sub Test_Child(ByVal Temp_WS as Worksheet)
    debug.print Temp_WS.Example_Button.Value
End Sub

The above triggers the error on the debug.print line. Whereas the following works without issue:

Sub Test2
    debug.print Sheet1.Example_Button.Value
End Sub

My gut says that this is because in the second case the compiler looks at a direct reference (e.g. Sheet1) and can see that the button exists as a member, whereas in the first case it looks at the variable and views it as a generic worksheet type (rather than being Sheet1) and so doesn't think there would be this member? Or maybe I'm completely off the mark?

Either way, is there a way around it? (Or am I doing something stupid for that matter?) If not it's not the end of the world, but I typically like to minimise the number of hard-coded references to sheets so if this is a way to resolve it then that would be great.

答案1

得分: 3

Example_Button 作为一个 ActiveX OLEObjectSheet1 的成员,可以通过 Sheet1.Example_Button 来引用。

但它也是一个 OLEObject 和一个 shape

你无法在一般的 Worksheet 对象中找到它。你可以使用如下的 Sub,通常声明参数为 As Variant(或 As Object),传递 Sheet1 类本身:

Sub Test_Child(ByVal Temp_WS As Variant)
    Debug.Print Temp_WS.Example_Button.Value
End Sub

这将起作用。实际上,声明为 As VariantAs Object 可以模拟下面的 Sub,允许声明参数为涉及的工作表类别。唯一一个以你尝试的方式公开对象的子程序是:

Sub Test_ChildSheetSh1(ByVal Temp_WS As Sheet1)
    Debug.Print Temp_WS.Example_Button.Value
End Sub

但更好的方法是将对象作为 OLEObjectshape 使用,这对于 Worksheet 对象也适用:

Sub Test_ChildOLE(ByVal Temp_WS As Worksheet)
    Debug.Print Temp_WS.OLEObjects("Example_Button").Object.Value
End Sub

或者

Sub Test_ChildSh(ByVal Temp_WS As Worksheet)
    Debug.Print Temp_WS.Shapes("Example_Button").OLEFormat.Object.Object.Value
End Sub

你甚至可以将 Worksheet 用于下面的方式的 Form 类型按钮(不是 ActiveX):

Sub Test_ChildForm(ByVal Temp_WS As Worksheet)
    Debug.Print Temp_WS.Shapes("Example_Button").OLEFormat.Object.Caption
End Sub
英文:

Example_Button, as an ActiveX OLEObject is a member of Sheet1 and it can be referenced as Sheet1.Example_Button.

But it is also an OLEObject and a shape, too.

You cannot find it as a member of the general Worksheet object. You can use the Sub as you try, generally declaring the parameter As Variant (or As Object), passing the Sheet1 class itself:

Sub Test_Child(ByVal Temp_WS as Variant)
    Debug.Print Temp_WS.Example_Button.Value
End Sub

And this will work. Practically, declaring As Variant or As Object it simulates the next Sub, which allows declaring he parameter as the involved sheet class. The single one exposing the object in the way you try:

Sub Test_ChildSheetSh1(ByVal Temp_WS As Sheet1)
    Debug.Print Temp_WS.Example_Button.Value
End Sub

But better approaches are to use the object as OLEObject or as shape, which works for Worksheet object, too:

Sub Test_ChildOLE(ByVal Temp_WS as Worksheet)
    Debug.Print Temp_WS.OLEObjects("Example_Button").Object.Value
End Sub

or

Sub Test_ChildSh(ByVal Temp_WS As Worksheet)
    Debug.Print Temp_WS.Shapes("Example_Button").OLEFormat.Object.Object.Value
End Sub

You can event use Worksheet for a Form type button (not an ActiveX) in the next way:

Sub Test_ChildForm(ByVal Temp_WS As Worksheet)
    Debug.Print Temp_WS.Shapes("Example_Button").OLEFormat.Object.Caption
End Sub

huangapple
  • 本文由 发表于 2023年8月4日 01:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定