我做错了什么?
sheet1.activate
activesheet.range(cells(2,"Q"),cells(40,"K")).select 'here the error occurs
selection.copy
上面的代码有时有效,有时会抛出一个错误,对象变量未设置,这意味着什么?为什么上面的代码不起作用,如果我重新打开文件,它会再次起作用,有时却不起作用。但我不知道发生这种情况的原因。我在语法中犯了任何错误吗?如果有,请告诉我正确的方法。有没有更有效的方法来选择一组范围?除了上述以外?先感谢您
sheet1.activate
activesheet.range(cells(2,"Q"),cells(40,"K")).select 'here the error occurs
selection.copy
The above code works for somtimes and for sometimes throws an Error that with object variable not set what does that mean ? why the above code is not working and if i reopen the file it works again and sometimes dont. But i dont know the reason why it happens. Am i making any mistake in the syntax, If so please let me know the right way to do it. And is there any more efficient way to do to select a set of range ? other than the above ? Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个错误的原因通常是遗漏了
Set
关键字,例如,如果您告诉我们错误发生在哪一行,那么将会得到更具体的答案。您的代码示例的语法看起来不错,但错误可能是由于代码中的另一行造成的,可能没有在您的问题中显示。
有没有更“有效”的方法来选择范围?就我个人而言,我不喜欢您展示的使用字符串作为
Cells
参数的方法。字符串杂乱,经常会导致错误,从而降低工作效率。为什么不:或者,将 K2:Q40 定义为工作表中的命名范围,将其称为“myRange”,然后执行以下操作:
但是为什么要使用
Select
?这通常是没有必要的。只是写:但是我可以问,为什么你使用
Copy
,这本身就是一个混乱的方法?...The cause of this error is usually the omission of a
Set
keyword, e.g.If you tell us on what line the error occurs, then a more specific answer will be forthcoming. The syntax of your code sample looks fine, but the error may be due to another line in your code, perhaps not shown in your question.
Is there any more "efficient" way to do to select a range? Personally, I don't like the method you show, with strings as the arguments for
Cells
. Strings are messy, and often lead to mistakes and therefore a loss of work efficiency. Why not:Or, define K2:Q40 as a named range in your sheet, calling it e.g. "myRange", and do this:
But why are you using
Select
? This is usually not necessary. Just write:But then I could ask, why are you using
Copy
, which is a messy method in its own right?...