Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将值从函数传递到子例程_Excel_Vba_Function_Ms Access_Subroutine - Fatal编程技术网

Excel 将值从函数传递到子例程

Excel 将值从函数传递到子例程,excel,vba,function,ms-access,subroutine,Excel,Vba,Function,Ms Access,Subroutine,我一直在四处寻找答案,但一直找不到一个能充分解释这一点的答案 我没有函数方面的经验,而且基本上没有VBA方面的经验。因此,如果我有一个正在执行的子程序,然后调用一个传递参数的函数,一旦该函数运行,我如何将结果返回到要使用的主子程序中 这是为了访问,我从一个记录集中提取一个数字,然后将其传递给一个函数,用于插入以创建一个新的数字。然后,我需要将该数字传回子例程以供使用。这里有两种选择 使用F8键运行代码,查看发生了什么,并确保激活,以便查看变量值的变化 1-直接从函数返回值 运行dosometh

我一直在四处寻找答案,但一直找不到一个能充分解释这一点的答案

我没有函数方面的经验,而且基本上没有VBA方面的经验。因此,如果我有一个正在执行的子程序,然后调用一个传递参数的函数,一旦该函数运行,我如何将结果返回到要使用的主子程序中


这是为了访问,我从一个记录集中提取一个数字,然后将其传递给一个函数,用于插入以创建一个新的数字。然后,我需要将该数字传回子例程以供使用。

这里有两种选择

使用
F8
键运行代码,查看发生了什么,并确保激活,以便查看变量值的变化


1-直接从函数返回值

运行
dosomethingreturnfrom函数
sub

' Return value from function
Public Sub DoSomethingReturnFromFunction()
    
    ' Define a parameter
    Dim myParameter As String
    myParameter = "SomeValue"
    
    ' Call a function and store its value into a variable
    Dim myResult As Long
    myResult = MyFunction(myParameter)
    
    ' Print the result variable
    Debug.Print myResult

End Sub

Private Function MyFunction(ByVal myParameter as String) As Long

    Dim result As Long

    Select Case myParameter
    Case "SomeValue"
        result = 1
    Case Else
        result = 2
    End Select
    
    ' Assign the result value to the function
    MyFunction = result

End Function
' Change variable value passed ByRef inside another function
Public Sub DoSomethingReturnFromByRef()
    
    ' Call a function
    Dim myByRefParameter As Long
    MySub myByRefParameter
    
    ' Print the result
    Debug.Print myByRefParameter

End Sub

Private Sub MySub(ByRef myByRefParameter As Long)
    
    ' Change the value of the variable passed ByRef inside the procedure
    myByRefParameter = 1

End Sub
结果:
Debug.Print myResult
将函数返回的值打印到中间窗口


2-更改在另一个函数中由ref传递的变量值

从ByRef运行
DoSomethingReturnFromByRef
sub

' Return value from function
Public Sub DoSomethingReturnFromFunction()
    
    ' Define a parameter
    Dim myParameter As String
    myParameter = "SomeValue"
    
    ' Call a function and store its value into a variable
    Dim myResult As Long
    myResult = MyFunction(myParameter)
    
    ' Print the result variable
    Debug.Print myResult

End Sub

Private Function MyFunction(ByVal myParameter as String) As Long

    Dim result As Long

    Select Case myParameter
    Case "SomeValue"
        result = 1
    Case Else
        result = 2
    End Select
    
    ' Assign the result value to the function
    MyFunction = result

End Function
' Change variable value passed ByRef inside another function
Public Sub DoSomethingReturnFromByRef()
    
    ' Call a function
    Dim myByRefParameter As Long
    MySub myByRefParameter
    
    ' Print the result
    Debug.Print myByRefParameter

End Sub

Private Sub MySub(ByRef myByRefParameter As Long)
    
    ' Change the value of the variable passed ByRef inside the procedure
    myByRefParameter = 1

End Sub
结果:
Debug.Print myByRefParameter
将存储在
myByRefParameter
过程中最初声明的
DoSomethingReturnFromByRef
变量中的值打印到中间窗口


如果清楚的话请告诉我这里有两种选择

使用
F8
键运行代码,查看发生了什么,并确保激活,以便查看变量值的变化


1-直接从函数返回值

运行
dosomethingreturnfrom函数
sub

' Return value from function
Public Sub DoSomethingReturnFromFunction()
    
    ' Define a parameter
    Dim myParameter As String
    myParameter = "SomeValue"
    
    ' Call a function and store its value into a variable
    Dim myResult As Long
    myResult = MyFunction(myParameter)
    
    ' Print the result variable
    Debug.Print myResult

End Sub

Private Function MyFunction(ByVal myParameter as String) As Long

    Dim result As Long

    Select Case myParameter
    Case "SomeValue"
        result = 1
    Case Else
        result = 2
    End Select
    
    ' Assign the result value to the function
    MyFunction = result

End Function
' Change variable value passed ByRef inside another function
Public Sub DoSomethingReturnFromByRef()
    
    ' Call a function
    Dim myByRefParameter As Long
    MySub myByRefParameter
    
    ' Print the result
    Debug.Print myByRefParameter

End Sub

Private Sub MySub(ByRef myByRefParameter As Long)
    
    ' Change the value of the variable passed ByRef inside the procedure
    myByRefParameter = 1

End Sub
结果:
Debug.Print myResult
将函数返回的值打印到中间窗口


2-更改在另一个函数中由ref传递的变量值

从ByRef运行
DoSomethingReturnFromByRef
sub

' Return value from function
Public Sub DoSomethingReturnFromFunction()
    
    ' Define a parameter
    Dim myParameter As String
    myParameter = "SomeValue"
    
    ' Call a function and store its value into a variable
    Dim myResult As Long
    myResult = MyFunction(myParameter)
    
    ' Print the result variable
    Debug.Print myResult

End Sub

Private Function MyFunction(ByVal myParameter as String) As Long

    Dim result As Long

    Select Case myParameter
    Case "SomeValue"
        result = 1
    Case Else
        result = 2
    End Select
    
    ' Assign the result value to the function
    MyFunction = result

End Function
' Change variable value passed ByRef inside another function
Public Sub DoSomethingReturnFromByRef()
    
    ' Call a function
    Dim myByRefParameter As Long
    MySub myByRefParameter
    
    ' Print the result
    Debug.Print myByRefParameter

End Sub

Private Sub MySub(ByRef myByRefParameter As Long)
    
    ' Change the value of the variable passed ByRef inside the procedure
    myByRefParameter = 1

End Sub
结果:
Debug.Print myByRefParameter
将存储在
myByRefParameter
过程中最初声明的
DoSomethingReturnFromByRef
变量中的值打印到中间窗口


请告诉我是否清楚

您可以通过函数传递参数
ByRef
或直接返回值您可以提供一个小示例吗?我是一个视觉人,所以视觉帮助很大。你可以通过函数传递参数
ByRef
,或者直接返回值。你能提供一个小例子吗?我是一个视觉化的人,所以视觉帮助很大。这非常详细。我想我理解它,并会尝试它,看看我是否能做到正确。这非常详细。我想我理解它,并会尝试它,看看我是否能做到正确。