excel vba调用带变量的子程序
我定义了以下子例程:
Sub EnterCellValueMonthNumber(cells As range, number As Integer)
range(cells).Select
ActiveCell.FormulaR1C1 = number
End Sub
当我像这样调用子例程时:
EnterCellValueMonthNumber ("N23:Q23",1)
我收到以下错误消息:
Compile error Expected: =
我不知道为什么收到此消息。有谁知道我缺少什么?
I defined the following subroutine:
Sub EnterCellValueMonthNumber(cells As range, number As Integer)
range(cells).Select
ActiveCell.FormulaR1C1 = number
End Sub
When I call the subroutine like this:
EnterCellValueMonthNumber ("N23:Q23",1)
I get the following error message:
Compile error Expected: =
I have no idea why I get this message. Does anyone know what I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你实际上有两个问题。
首先是你问题的实际答案。你需要说:
但是如果你运行它,这仍然不起作用,因为你已经设置了
range
类型的cells
...将其设置为string
类型代码> 使用这个代替:You actually have 2 issues.
First the actual answer to your question. You need to say:
But that still wont work if you run it, since you have set
cells
of typerange
... set it as typestring
using this instead:尝试以下代码:
Try the following code:
您可以将 sub 称为
无括号。或者
括号,以及前面的
Call
。另外,您的 Sub 需要一个 Range 对象作为第一个参数,并且您提供一个字符串;您应该将 sub 的签名更改为:
另外,我不确定您要使用此代码实现什么目的,因为它只会将范围的左上角单元格设置为 1。是否会
更合适?
我还非常谨慎地使用
Range("...")
而不指定您所指的工作表。这将作用于活动工作表,因此可能会导致意外问题,几乎总是首选SheetX.Range("...")
。同样,对于使用.Select
来说,这是不必要的,并且只会在将来给您带来问题。You would call the sub as
No brackets. Or
Brackets, and
Call
before it.Also, your Sub is expecting a Range object as the first argument and you're supplying a string; you should change the signature of the sub to:
Also, I'm uncertain as to what you are trying to achieve with this code as it will only set the top-left cell of the range to 1. Would something like
be more appropriate?
I'd also be very wary of using
Range("...")
without specifying which sheet you are referring to. This will act on whichever is the active sheet and can thus cause unexpected problems, almost always preferSheetX.Range("...")
. Similarly for using.Select
, it's unnecessary, and can only cause problems for you in the future.