【问题标题】:How do Iinvoke a VBA function inside a Worksheet?如何在工作表中调用 VBA 函数?
【发布时间】:2019-05-11 17:20:26
【问题描述】:

我有一个简单的 VBA 函数,它保存在受信任的启用宏的工作簿的 ThisWorkbook 中。此函数的代码:GetParameterKey 如下。

我想从同一工作簿中的工作表调用 GetParameterKey 作为电子表格函数。但是,GetParameterKey 不会在插入函数下显示为用户定义的函数。

有没有办法从同一工作簿中的工作表调用在 ThisWorkbook 部分中定义的函数?

Public Function GetParameterKey(natureOfWork As String, size As String, complexity As String, uncertainty As String) As String
'
' GetParameterKey Macro
' Computes a VLOOKUP key for Nature of Work, Size, Complexity, and Uncertainty.
'
    Select Case UCase(Trim(natureOfWork))

        Case "BACK END"
            GetParameterKey = "1"

        Case "FRONT END"
            GetParameterKey = "2"

        Case "BOTH"
            GetParameterKey = "3"

        Case Else
            GetParameterKey = "0"

    End Select

    GetParameterKey = GetParameterKey & CategoryKey(size)
    GetParameterKey = GetParameterKey & CategoryKey(complexity)
    GetParameterKey = GetParameterKey & CategoryKey(uncertainty)

End Function

Function CategoryKey(category As String) As String

    Select Case UCase(Trim(category))

        Case "VERY LARGE"
            CategoryKey = "5"

        Case "LARGE"
            CategoryKey = "4"

        Case "MEDIUM"
            CategoryKey = "3"

        Case "SMALL"
            CategoryKey = "2"

        Case Else
            CategoryKey = "1"

    End Select

End Function

【问题讨论】:

标签: excel vba


【解决方案1】:

将函数放在单独的模块中。

  1. 插入 -> 模块
  2. 将函数粘贴到那里(确保它被声明为Public
  3. 您现在应该可以从工作表中调用它了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多