简体   繁体   English

Excel VBA如何从函数返回变量

[英]Excel VBA how to return variable from function

Example of the code: 代码示例:

Sub

   Dim NOSSPrefix As String
   NOSSPrefix = Cells(1, 6).Value
   NOSSSpecific(NOSSPrefixSpec)

End Sub

Function NOSSSpecific(NOSSPrefixSpec)

   If NOSSPrefixSpec = "6" Or NOSSPrefixSpec = "17" Or NOSSPrefixSpec = "19" Then
       NOSSPrefix = NOSSPrefixSpec        
   Else
       NOSSPrefix = "999"
   End If

End Function

Tried alreaddy return, set, ... Just want to return the NOSSPrefix back from the function to the routine above. 尝试了alreaddy return,set,...只想将NOSSPrefix从函数返回到上面的例程。

Something like this: 像这样的东西:

Sub Main()
    Dim NOSSPrefixSpec As String
    NOSSPrefixSpec = "12"
    Debug.Print NOSSSpecific(NOSSPrefixSpec) //Returns "999"
End Sub

Function NOSSSpecific(NOSSPrefixSpec As String) As String
    If NOSSPrefixSpec = "6" Or NOSSPrefixSpec = "17" Or NOSSPrefixSpec = "19" Then
        NOSSSpecific = NOSSPrefixSpec
    Else
        NOSSSpecific = "999"
    End If
End Function

Note: 注意:

  • You have to define NOSSPrefix . 您必须定义NOSSPrefix I changed it to NOSSPrefixSpec 我把它改成了NOSSPrefixSpec
  • I've explicitly added String as the function argument type and return value 我已经明确地将String添加为函数参数类型并返回值

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

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