[英]“Method or Data Member Not Found” when Dim'ing Worksheet as Worksheet but not Variant
Consider this simple example. 考虑这个简单的例子。 In a new sheet create a ActiveX Checkbox called Checkbox1
在新工作表中创建一个名为Checkbox1的ActiveX复选框
Try the following two subroutines. 请尝试以下两个子例程。 The first does not compile with a "Method or Data Member Not Found" error, the second one works fine.
第一个没有编译“找不到方法或数据成员”错误,第二个工作正常。
Why doesn't the first example work? 为什么第一个例子不起作用?
Option Explicit
Sub DoesntWork()
Dim ws As Worksheet
Set ws = Worksheets(1)
MsgBox "Checkbox state is: " + CStr(ws.CheckBox1.Value)
End Sub
Sub Works()
Dim ws As Variant
Set ws = Worksheets(1)
MsgBox "Checkbox state is: " + CStr(ws.CheckBox1.Value)
End Sub
The problem is with the line ws.CheckBox1.Value
. 问题在于行
ws.CheckBox1.Value
。 You can't use it like this and hence you are getting that error. 你不能像这样使用它,因此你得到了这个错误。 Try this
试试这个
Sub Sample()
Dim ws As Worksheet
Dim objole As OLEObject
Set ws = Worksheets(1)
Set objole = ws.OLEObjects("CheckBox1")
MsgBox "Checkbox state is: " & objole.Object.Value
End Sub
If you want to use the Object directly then you can also use this 如果您想直接使用Object,那么您也可以使用它
Sub Sample()
Dim ws As Worksheet
Set ws = Worksheets(1)
MsgBox "Checkbox state is: " & Worksheets(ws.Name).CheckBox1.Value
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.