Home » excel » excel – How do Iinvoke a VBA function inside a Worksheet?

excel – How do Iinvoke a VBA function inside a Worksheet?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a simple VBA function that is saved in ThisWorkbook of a trusted Macro-Enabled Workbook. The code for this function: GetParameterKey is below.

I would like to call GetParameterKey as a spreadsheet function from a worksheet in the same workbook. However, GetParameterKey does not appear as a user-defined function under Insert Function.

Is there a way to call a function that is defined in the ThisWorkbook section from a worksheet in the same workbook?

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
How to&Answers:

Place the function in a separate module instead.

  1. Insert -> Module
  2. Paste the function there (make sure it’s declared as Public)
  3. And you should be able to call it from worksheet now

enter image description here
enter image description here