自定义函数在 Excel VBA 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14847449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:35:44  来源:igfitidea点击:

Custom functions not working in Excel VBA

excelfunctionvba

提问by Marc L

For some reason custom functions don't seem to be running in Excel. I created the simple function below but it always returns zero as a value whether it is used as a worksheet function or called from a procedure. Anyone know what I am doing wrong or if there is a setting somewhere I am missing?

出于某种原因,自定义函数似乎没有在 Excel 中运行。我创建了下面的简单函数,但无论它是用作工作表函数还是从过程调用,它始终返回零作为值。任何人都知道我做错了什么,或者是否有我遗漏的设置?

  Public Function Testthisout(number As Double) As Double
    result = number * number
  End Function

  Public Sub TESTFUNCTION()
    Dim number As Double
    Dim result As Double

    Application.Volatile (True)

    number = 4
    result = Testthisout(number)
    MsgBox result
  End Sub

采纳答案by Andrey Gordeev

Change you function to:

将您的功能更改为:

Public Function Testthisout(number As Double) As Double
  result = number * number
  Testthisout = result
End Function