VBA“参数不可选” - 不确定如何声明变量(VBA “Argument Not Optional

2019-10-31 04:50发布

我试图写VBA宏通过计算正上方和正下方的电池的平均值分配给特定的细胞。 我选择的开发者工具栏上的宏按钮运行它,然后我在我的函数的名称输入(它不会出现在名单上)“interpprob”并选择运行。 然后我得到的是指明了弹出式“的说法是不可选的。” 我不太清楚是什么问题。 全宏如下。 “TSTEP”是指能在设定了需要改变了一些细胞值的行的阵列。

Function interpprob(f As Integer, d As Integer, spec As String, tstep As Long, above As Long, below As Long, i As Integer, j As Integer)

f = 41
d = 441
spec = ETHA

tstep(0) = f
tstep(1) = f + d
tstep(2) = f + 2 * d
tstep(3) = f + 5 * d

For i = 0 To 4
    For j = 52 To 57
        above = Cells(tstep(i) - 1, j).Value
        below = Cells(tstep(i) + 1, j).Value
        Sheets(spec).Cells(tstep(i), j).Value = (above + below) / 2
    Next j
Next i

End Function

谢谢,BL袋鼠

Answer 1:

根据您的期望,改变FunctionSub和删除的参数。

Sub interpprob()

    f = 41
    d = 441
    spec = "ETHA"

    tstep(0) = f
    tstep(1) = f + d
    tstep(2) = f + 2 * d
    tstep(3) = f + 5 * d

    For i = 0 To 3  'Changed from 4 as you do not assign a value to tstep(4)
        For j = 52 To 57
            above = Cells(tstep(i) - 1, j).Value
            below = Cells(tstep(i) + 1, j).Value
            Sheets(spec).Cells(tstep(i), j).Value = (above + below) / 2
        Next j
    Next i

End Sub

您还可以插入下面的声明只是在后Sub

Dim f As Long
Dim d As Long
Dim spec As String
Dim tstep(0 To 3) As Long
Dim above As Long
Dim below As Long
Dim i As Long
Dim j As Long

这是当一个程序的增长,其回报的做法。 它使你从几种错误的安全。

为了使这种做法强制性的,插入以下指令作为文件(只是一切之前)的第一行:

Option Explicit

您还可以看到该类型Integer改为Long ,因为Integer是太短(-32768 ... +32767)和不切实际的标准使用,并保持双方围绕IntegerLong有没有真正的好处(和有性能损失)。 只需要声明的每个整数变量Long

对于建议和修正积分去YowE3K和robinCTS 。



文章来源: VBA “Argument Not Optional” - unsure how to declare variables