1

I am trying to return a value from a function but is not happening.

Public sub test()
  x = 2
  msgbox number(x)
Exit Sub

Public Function number(num)
  if len(num) = 1 then
     num = "0" + num
  End if
End Function

The function is returning a null value.

2 Answers 2

4

You need to assign the result to the name of the function, and concatenate the 0, like this:

Public Function number(num) As String
  if len(num) = 1 then
     number = "0" & num
  Else
     number = num
  End if
End Function

though it would seem easier to just use Format(num, "00")

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it worked for me This was just an example for what i wanted to code as the original code has loads of confidential data.
2
Public function number(num as string) as string
If len(num) = 1 then
    Number ="0" & num
Else 
    Number = num
End if
End function

You need to have code in the function that sets a value to the name of the function - the return value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.