简体   繁体   English

VBA中的语法错误

[英]Syntax error in VBA

I'm having issue with VBA code in Excel. 我在Excel中遇到VBA代码问题。 Here's my code 这是我的代码

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 , lowerbound and upperbound . 如您所见, generateMatrix()用于将随机整数矩阵返回到活动工作表,该工作表使用参数: sizelowerboundupperbound In taskOne() sub I'm using that generateMatrix() , specifying arguments correctly, but when I run that taskOne() sub, I get Compiler error: Syntax error and somewhy it highlights Sub taskOne() line as if the issue would be in that line... taskOne()子我正在使用generateMatrix() ,正确指定参数,但是当我运行那个taskOne()子时,我得到Compiler error: Syntax error ,为什么它突出显示Sub taskOne()行,好像问题是那条线......

What would be the reason of such issue? 这样的问题会是什么原因?

Thanks. 谢谢。

You define a Sub rather than a function. 您定义Sub而不是函数。 Try this: 尝试这个:

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

You could also do this: 你也可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM