VBA 中的语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20552582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:55:58  来源:igfitidea点击:

Syntax error in VBA

excelvbaexcel-vba

提问by Aleksei Sorokin

I'm having issue with VBA code in Excel. Here's my code

我在 Excel 中遇到 VBA 代码问题。这是我的代码

Sub generateMatrix(size, lowerbound, upperbound)
    For i = 1 To size
        For j = 1 To size
            ActiveSheet.Cells(i, j).Value = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
        Next j
    Next i
End Sub

Sub taskOne()
    generateMatrix(5, -100, 50)
End Sub

As you can see, generateMatrix()is being used to return matrix of random integer numbers to active worksheet, that uses arguments: size, lowerboundand upperbound. In taskOne()sub I'm using that generateMatrix(), specifying arguments correctly, but when I run that taskOne()sub, I get Compiler error: Syntax errorand somewhy it highlights Sub taskOne()line as if the issue would be in that line...

如您所见,generateMatrix()用于将随机整数矩阵返回到活动工作表,该工作表使用参数:size,lowerboundupperbound。在taskOne()sub 中,我正在使用那个generateMatrix(),正确指定参数,但是当我运行该taskOne()sub 时,我得到Compiler error: Syntax error并且不知何故它突出显示了Sub taskOne()一行,好像问题就在该行中......

What would be the reason of such issue?

出现这种问题的原因是什么?

Thanks.

谢谢。

回答by Roger Rowland

You define a Subrather than a function. Try this:

您定义一个Sub而不是一个函数。尝试这个:

Sub taskOne()
    generateMatrix 5, -100, 50
End Sub

You could also do this:

你也可以这样做:

Sub taskOne()
    Call generateMatrix(5, -100, 50)
End Sub