简体   繁体   English

将多个参数从一个子传递到另一个子

[英]pass multiple argument from one sub to another sub

I am trying to pass multiple arguments from one sub to another sub, but it's not working. 我正在尝试将多个参数从一个子传递到另一个子,但是它不起作用。 Even if I try to define variable as string. 即使我尝试将变量定义为字符串。

Sub run
    Dim uniqueId, errorMessage, jobId, ErrorCode
    uniqueId = "abcc"
    jobId = "efgh"
    ErrorCode = "ijkl"
    errorMessage = "mnop"
    DisplayCustomError errorMessage, uniqueId, jobId, ErrorCode
End Sub

Sub DisplayCustomError(errorMessage, uniqueId, jobId, ErrorCode)
    WScript.Echo uniqueId
    WScript.Echo jobId
    WScript.Echo ErrorCode
    WScript.Echo errorMessage
End Sub

VBA and VBScript are a bit different. VBAVBScript有点不同。

But for VBA the following code will work perfectly. 但是对于VBA ,以下代码将完美运行。

A few pointers: 一些提示:

  • You always wants to Dim all your variables, I understand this is probably just a quick example you provided but I felt to mentioned it. 您始终想对所有变量进行调Dim ,我知道这可能只是您提供的一个简单示例,但我想提到它。

  • When receiving variables in a Sub you always want to define them in the Sub which is receiving them as to make correct use of the variable in the proceeding code. Sub接收变量时,您总是要在要接收它们的Sub定义它们,以便在后续代码中正确使用variable

  • Have a look here for a question I asked about only passing certain variables to a Sub . 在这里看看我提出的仅将某些variables传递给Sub This might help when you only want to pass one or two variables to a "receiving" Sub 当您只想将一个或两个variables传递给“接收” Sub时,这可能会有所帮助

     Option Explicit Sub run() Dim uniqueId As String Dim errorMessage As String Dim jobId As String Dim ErrorCode As String uniqueId = "abcc" jobId = "efgh" ErrorCode = "ijkl" errorMessage = "mnop" DisplayCustomError errorMessage, uniqueId, jobId, ErrorCode End Sub Sub DisplayCustomError(ByVal errorMessage As String, ByVal uniqueId As String, _ ByVal jobId As String, ByVal ErrorCode As String) Debug.Print uniqueId Debug.Print jobId Debug.Print ErrorCode Debug.Print errorMessage End Sub 

It has to be Call DisplayCustomError(...) because its a sub, only functions are called withouth anything in front. 它必须Call DisplayCustomError(...)因为它是一个子函数,仅在前面没有任何函数的情况下调用函数。 Also: 也:

Sub DisplayCustomError(ByVal errorMessage as String, ByVal uniqueId as String, ByVal jobId as String, ByVal ErrorCode as String)

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

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