对象必需错误 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12305171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object Required error Excel VBA
提问by Ravi
I am creating a VBA application, and I have the following code:
我正在创建一个 VBA 应用程序,我有以下代码:
Dim previousCell As range
Private Sub Worksheet_SelectionChange(ByVal target As range)
Application.EnableEvents = False
On Error GoTo ws_exit:
Set previousCell = target
getEffort (previousCell) '**Here i get object required**
ws_exit:
Application.EnableEvents = True
MsgBox Err.Description
End Sub
Private Function getEffort(ByVal cell As range)
' do soemthing
End Sub
I'm not sure why I get the error message: Object required error at getEffort(previousCell)
. If I pass in the Target
, it works.
我不确定为什么会收到错误消息: Object required error at getEffort(previousCell)
. 如果我传入Target
,它会起作用。
Thanks
谢谢
回答by mwolfe02
As others have suggested, the problem is the parentheses. What no one has adequately explained is why it is the parentheses.
正如其他人所建议的那样,问题在于括号。没有人充分解释的是为什么它是括号。
When you say this:
当你这样说时:
getEffort previousCell
Then you are passing the previousCell
Range objectinto the getEffort procedure. That is what the procedure expects and so it is happy.
然后您将previousCell
Range 对象传递到 getEffort 过程。这是程序所期望的,因此很高兴。
When you say this:
当你这样说时:
getEffort (previousCell)
The parentheses around previousCell
cause VBA to evaluatethe previousCell
object. When VBA evaluatesan object it returns that object's default property. The default property of the Range object is .Value
, which is a string.
周围的括号previousCell
原因VBA来评估的previousCell
对象。当 VBA评估一个对象时,它会返回该对象的默认属性。Range 对象的默认属性是.Value
,它是一个字符串。
So previousCell is evaluated and a stringgets passed on to getEffort. Of course getEffort is expecting a Range object, so you receive the error message.
所以previousCell 被评估并且一个字符串被传递给getEffort。当然, getEffort 需要一个 Range 对象,因此您会收到错误消息。
The fact that you are assigning Target
to previousCell
is a red herring. You likely introduced the parentheses when you switched to previousCell
. If you don't believe me, try this:
你分配Target
给的事实previousCell
是一个红鲱鱼。当您切换到previousCell
. 如果你不相信我,试试这个:
getEffort (Target)
You will get the same error message.
您将收到相同的错误消息。
回答by d4v3y0rk
It looks like target is not set to an instance of an object which is giving you the error. when you pass in the target the argument to the function function(argument) is set to an instance of an object. when you set previouscell = target target actually needs to be something or else you will get the exec error.
看起来目标未设置为给您错误的对象的实例。当您传入目标时,函数 function(argument) 的参数被设置为对象的实例。当您设置 previouscell = target target 实际上需要是某物时,否则您将收到 exec 错误。
try setting previouscell = ActiveCell
尝试设置 previouscell = ActiveCell
回答by enderland
Two things: first, you need to not use ()
or include some sort of return value when calling getEffort as a function. You also need to determine if you want that to be a sub/function, right now you are using both. Presumably you are making it a sub?
两件事:首先,()
在将 getEffort 作为函数调用时,您不需要使用或包含某种返回值。您还需要确定您是否希望它成为一个子/功能,现在您正在同时使用两者。想必你是把它变成一个潜艇?
Dim previousCell As range
Private Sub Worksheet_SelectionChange(ByVal target As range)
Application.EnableEvents = False
On Error GoTo ws_exit:
Set previousCell = target
getEffort previousCell '**Here i get object required**
'or...
call getEffort(previousCell)
'add this too..
'exit sub
ws_exit:
Application.EnableEvents = True
MsgBox Err.Description
End Sub
Private sub getEffort(ByVal cell As range)
' do soemthing
End sub
Also, your main program never exits before your error statement, so it will pop up that message box always. Try adding Exit Sub
before the error label to avoid a blank message box always appearing.
此外,您的主程序永远不会在您的错误语句之前退出,因此它会始终弹出该消息框。尝试Exit Sub
在错误标签之前添加以避免总是出现空白消息框。