excel vba调用带变量的子程序

发布于 2024-11-14 14:30:57 字数 360 浏览 8 评论 0原文

我定义了以下子例程:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的温暖 2024-11-21 14:30:58

你实际上有两个问题。

首先是你问题的实际答案。你需要说:

Call EnterCellValueMonthNumber("N23:Q23",1)

但是如果你运行它,这仍然不起作用,因为你已经设置了 range 类型的 cells...将其设置为 string 类型代码> 使用这个代替:

Sub EnterCellValueMonthNumber(cells As String, number As Integer)
    Range(cells).Select
    ActiveCell.FormulaR1C1 = number
End Sub

You actually have 2 issues.

First the actual answer to your question. You need to say:

Call EnterCellValueMonthNumber("N23:Q23",1)

But that still wont work if you run it, since you have set cells of type range... set it as type string using this instead:

Sub EnterCellValueMonthNumber(cells As String, number As Integer)
    Range(cells).Select
    ActiveCell.FormulaR1C1 = number
End Sub
贪恋 2024-11-21 14:30:58

尝试以下代码:

Sub EnterCellValueMonthNumber(ByVal cells As String, number As Integer)

    Range(cells).Select

    ActiveCell.FormulaR1C1 = number

End Sub

Try the following code:

Sub EnterCellValueMonthNumber(ByVal cells As String, number As Integer)

    Range(cells).Select

    ActiveCell.FormulaR1C1 = number

End Sub
难以启齿的温柔 2024-11-21 14:30:57

您可以将 sub 称为

EnterCellValueMonthNumber "N23:Q23", 1

无括号。或者

Call EnterCellValueMonthNumber("N23:Q23", 1)

括号,以及前面的Call

另外,您的 Sub 需要一个 Range 对象作为第一个参数,并且您提供一个字符串;您应该将 sub 的签名更改为:

Sub EnterCellValueMonthNumber(cells As String, number As Integer)

另外,我不确定您要使用此代码实现什么目的,因为它只会将范围的左上角单元格设置为 1。是否会

Range(cells).Value = number
' Or, if you're going to be passing in something more complex later...
Range(cells).FormulaR1C1 = number

更合适?

我还非常谨慎地使用 Range("...") 而不指定您所指的工作表。这将作用于活动工作表,因此可能会导致意外问题,几乎总是首选 SheetX.Range("...")。同样,对于使用 .Select 来说,这是不必要的,并且只会在将来给您带来问题。

You would call the sub as

EnterCellValueMonthNumber "N23:Q23", 1

No brackets. Or

Call EnterCellValueMonthNumber("N23:Q23", 1)

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:

Sub EnterCellValueMonthNumber(cells As String, number As Integer)

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

Range(cells).Value = number
' Or, if you're going to be passing in something more complex later...
Range(cells).FormulaR1C1 = number

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 prefer SheetX.Range("..."). Similarly for using .Select, it's unnecessary, and can only cause problems for you in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文