【问题标题】:Value of a user defined function用户定义函数的值
【发布时间】:2013-10-18 05:16:27
【问题描述】:

我在 Excel 2010 VBA 中有一个用户定义函数。

在单元格A1 中,我调用该函数:=myfunction() 并且返回的值(作为整数)在A1 单元格中正确显示。

我关心的是如何从 VBA 中的其他函数获取 A1 单元格的值。我想让MyOtherFunction()获取A1的数量。

单元格A1=MyFunction()

function MyFunction() as integer
   ......
   MyFunction = 99
End function

我在A1 中正确地看到了99 的值。

function MyOtherFunction()
   dim valor as integer
   valor =  Workbooks("xxx.xlsm").Sheets("yyy").Range("A1").Value
End function

在该函数中,变量 valor 返回 0 而不是 99

【问题讨论】:

  • 我的回答难懂吗?
  • 没有 Siddharth,谢谢,但返回值始终为零。我有这个: Workbooks("xxx.xlsm").Sheets("yyy").Range("A1").Value 我得到的值为 0 但 A1 单元格的结果为 =myfunction()
  • 你确定你在ThisWorkbook.Sheets("Sheet1")中传递了正确的表格吗?
  • 我看到你编辑了你的评论。您可以使用您正在使用的确切代码更新您的问题吗?我在测试后发布了代码。
  • 我写了代码,但不知道如何对齐才能正确显示!

标签: excel excel-2010 vba


【解决方案1】:
Function MyOtherFunction(Rng As Range) As Whatever
    '~~> Rest of the code
End Function

在工作表中作为 UDF

=MyOtherFunction(A1)

在 VBA 中

Sub Sample()
    Dim Ret
    Ret = MyOtherFunction(ThisWorkbook.Sheets("Sheet1").Range("A1"))
End Sub

【讨论】:

    【解决方案2】:

    你当然可以写:

    Function MyOtherFunction()
        MyOtherFunction = Range("A1").Value
    End Function
    

    还是我没抓住重点?

    【讨论】:

    • 谢谢,但返回值始终为零
    • 也许您指的不是正确的工作簿或工作表?
    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 2012-07-25
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多