Q:

VBA Get function value

How do I return the value of this vba function? If I add the debug.print to the function itself, it prints the correct value.
Sub testIt()
Debug.Print getAccountNumber("keyB")
End Sub

Function getAccountNumber(myKey)
Dim c As Collection
Set c = New Collection
c.Add "4005", "keyA"
c.Add "4015", "keyB"
c.Add "4030", "keyC"
c.Item (myKey)
End Function

A:

You have to set the return-value of the function somewhere within the function. In your example above, you'd do something like
getAccountNumber = c.Item (myKey)

Whatever is stored in getAccountNumber when you exit the function is what is returned to whatever called it. I'll also add that it is considered good practice to specify what the funtion returns, in you case you should probably define the function as Function getAccountNumber(myKey) as String.

A:

You return a value from a Function by assigning the result to the function name:
Function getAccountNumber(myKey)
    Dim c                     As Collection
    Set c = New Collection
    c.Add "4005", "keyA"
    c.Add "4015", "keyB"
    c.Add "4030", "keyC"
    getAccountNumber = c.Item(myKey)
End Function

