Excel VBA如何从函数返回变量

时间:2018-01-05 18:21:02

标签: excel-vba vba excel

代码示例:

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

尝试回归,设定,...... 只想将NOSSPrefix从函数返回到上面的例程。

1 个答案:

答案 0 :(得分:3)

这样的事情:

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

注意:

  • 您必须定义NOSSPrefix。我将其更改为NOSSPrefixSpec
  • 我已明确添加String作为函数参数类型并返回值