如何在工作表中调用VBA函数?

时间:2019-05-11 17:20:26

标签: excel vba

我有一个简单的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

1 个答案:

答案 0 :(得分:2)

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

  1. 插入->模块
  2. 将函数粘贴到那里(确保将其声明为$("select").select2({ // other options selectionAdapter: CustomSelectionAdapter, selectionContainer: $('.foo') });
  3. 您应该现在就可以从工作表中调用它

enter image description here enter image description here