试图了解如何将值从函数传递回子对象并在消息框中显示结果,但我无法理解。我在下面缺少什么学习代码:-

Sub mySend()

Dim myNum As Integer

myNum = InputBox("Enter number")

myReturn (myNum)

msgbox myReturn 'would like to display result in messagebox here but no joy

End Sub
'--------------------------------------
Function myReturn(ByRef myNum As Integer)

Dim myCalc As Integer

myCalc = myNum + 10

myReturn = myCalc

End Function

最佳答案

不要使用ByRef参数-只需正确使用函数即可。

Sub mySend()

Dim myNum As Integer

myNum = InputBox("Enter number")

myNum = myReturn(myNum) '// You need to assign (=) the value to the variable

msgbox myNum

End Sub
'--------------------------------------
Function myReturn(number As Integer) As Integer '// Note the return type after the ()

Dim myCalc As Integer

myCalc = number + 10

myReturn = myCalc

End Function

如果要在示例中通过引用传递变量,则需要实际更改该变量的值,否则当您在调用代码中再次引用该变量时,该值将保持不变:

(这是对您的代码进行了修改,以正确使用时显示ByRef的结果,我不建议您实际使用此代码)
Sub mySend()

Dim myNum As Integer

myNum = InputBox("Enter number")

myReturn myNum '// No need for parentheses here

msgbox myNum

End Sub
'--------------------------------------
Function myReturn(ByRef myNum As Integer)

myNum = myNum + 10

End Function

10-05 21:29