调用一个模块从另一个模块时出现问题。

huangapple go评论63阅读模式
英文:

Problem with calling a module from another module

问题

我在Excel 2016的Mac版本中使用VBA时,调用其他模块的模块时遇到了问题。
在一个工作簿中,我有2个模块:

Sub testsub()
    Call xchart(a, b, c)
    MsgBox a
End Sub

Sub xchart(a, b, c)
    a = b + c
End Sub

当我运行第一个模块(testsub)时,我收到一个"编译错误。期望变量或过程而不是模块"的错误消息。
我做错了什么?我在其他工作簿中的某些地方使用了这个结构,有时可以正常工作,有时会出现相同的编译错误。

英文:

I am having trouble calling modules from other modules using VBA in excel 2016 on a mac.
In a workbook I have 2 modules:

Sub testsub()
    Call xchart(a, b, c)
    MsgBox a
End Sub

and

Sub xchart(a, b, c)
    a = b + c
End Sub

When I run the first module(testsub) I get a "Compile error. Expected variable or procedure not a module"
What am I doing wrong? I have used this construct elsewhere in other workbooks and sometimes it works without a problem, sometimes it gives the same compile error.

答案1

得分: 2

调用一个过程

在VBA中,'those' 和 'these' 被称为 过程,它们位于 模块 中。
基本上,Sub 做一些事情,而 Function 返回一个结果。
Call 关键字被认为是已弃用的。
对于整数(整数(数学)),你可以使用类似以下的代码。

Option Explicit

Sub MyFirstCallingProcedure()

    Dim b As Long: b = 1
    Dim c As Long: c = 2
    
    Dim a As Long: a = SumupTwoIntegers(b, c)
    
    MsgBox a
    
End Sub

Function SumupTwoIntegers(ByVal Int1 As Long, ByVal Int2 As Long) As Long
    SumupTwoIntegers = Int1 + Int2
End Function
英文:

Calling a Procedure

  • In VBA, 'those' and 'these' are called Procedures, and they are located in Modules.
  • Basically, a Sub does something while a Function returns a result.
  • The Call keyword is considered deprecated.
  • For whole numbers (integers(math)), you could use something like the following.

<!-- language: lang-vb -->

Option Explicit

Sub MyFirstCallingProcedure()

    Dim b As Long: b = 1
    Dim c As Long: c = 2
    
    Dim a As Long: a = SumupTwoIntegers(b, c)
    
    MsgBox a
    
End Sub

Function SumupTwoIntegers(ByVal Int1 As Long, ByVal Int2 As Long) As Long
    SumupTwoIntegers = Int1 + Int2
End Function 

答案2

得分: 0

当模块名称与同一模块中的子程序名称相同时发生这种情况。

'检查并重命名包含此子程序的模块(如果具有相同名称)
Sub testsub()
调用 xchart(a, b, c)
MsgBox a
End Sub

'检查并重命名包含此子程序的模块(如果具有相同名称)
Sub xchart(a, b, c)
a = b + c
End Sub

英文:

This happens when the module name is the same with the Sub in the same module.

&#39;check and RENAME THE MODULE containing this Sub if have the same name
Sub testsub()  
    Call xchart(a, b, c)
    MsgBox a
End Sub

&#39;check and RENAME THE MODULE containing this Sub if have the same name
Sub xchart(a, b, c)
    a = b + c
End Sub

huangapple
  • 本文由 发表于 2023年5月29日 15:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定