2017-09-04 621 views
0

我正在尝试编写一个VBA宏,它通过计算直接在其上方和下方的单元格的平均值,将值分配给特定单元格。我通过选择开发工具栏上的宏按钮来运行它,然后我必须键入我的函数的名称(它不会出现在列表中)“interpprob”并选择运行。然后我得到一个弹出窗口,其中指出“参数不是可选的”。我不太确定问题是什么。完整的宏在下面。 “tstep”意味着需要更改一些单元格值的一组行的数组。VBA“参数不可选” - 不确定如何声明变量

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袋鼠

+0

确保您使用全部8个参数调用'interpprob'。 – miroxlav

+0

这将如何完成?只需输入“interpprob(f,d,spec,tstep,above,below,i,j)”? –

+0

您需要实际值(参数)来代替这8个参数。你有什么想法,你会把'f','d','spec'等放在什么位置? – miroxlav

回答

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 

您还可以看到该类型IntegerLong替换,因为Integer是太短(-32768 ... +32767),对于标准使用而言并不现实,并且保持IntegerLong都没有真正的好处(并且具有性能损失)。只需声明每个整数变量为Long

建议和修复的积分转到YowE3KrobinCTS

+1

现在使用'Long'而不是'Integer'是目前最好的练习,所以我不担心它(特别是考虑到OP使用了'Integer') - 我会+1原样。 – YowE3K

+0

我做了几个编辑,因为试图使用'tstep(4) - 1'作为一个行将崩溃。 – YowE3K

+0

感谢您提出这些建议,我相信我现在可以开展工作! –