3

I am a long time avid Excel user but am just starting to learn VBA. I am using the following code but am getting an error when I try to run Sub test:

Compile Error:Variable not defined

Can you help me figure out what is wrong?

Option Explicit

Function toFarenheit(degrees)
    toFarenheit = (9 / 5) * degrees + 32
End Function

Function toCentigrade(degrees)
    toCentigrade = (5 / 9) * degrees - 32
End Function

Sub test()
    answer = toCentigrade(55)
    MsgBox answer    
End Sub

3 Answers 3

7

You have Option Explicit turn on which means you must declare your variables before using them.

In Sub test, you are missing a declaration for answer. Adding this should fix it:

Sub test()
    Dim answer As Variant
    answer = toCentigrade(55)
    MsgBox answer    
End Sub

Edit

Since you are new to VBA, you might want to consider typing both your variables and function returns. You don't have to do this (and everything will be treated as a Variant), but it is good practice.

If you type everything properly, your example would become:

Option Explicit

' Accept a double value and return a double type value.
Function toFarenheit(degrees As Double) As Double
    toFarenheit = (9 / 5) * degrees + 32
End Function

Function toCentigrade(degrees As Double) As Double
    toCentigrade = (5 / 9) * degrees - 32
End Function

Sub test()
    ' Variable type matches what the function will return.
    Dim answer As Double
    answer = toCentigrade(55)
    MsgBox answer    
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

I would add that the custom Functions should be toWhatever(degrees As Long) As Long or whatever variable type you want. (not required but is always helpful to define variable type)
@Chrismas007 - I would agree. In the example provided by the OP, no type is defined for function return (which will return a Variant in Excel VBA) which is why I used Variant in my example.
@Chrismas007 - I updated the answer to include typing. This would be good info for someone new to VBA to consider anyway. Thanks for the suggestion.
-3

I tested this to convert to farenheit

The function is as follows

Function ToFarenheit(Degrees As Double)

ToFarenheit = (9 / 5) * Degrees + 32

End Function

The sub is as follows

Sub TestFunction()

MsgBox ToFarenheit(0)

End Sub

1 Comment

This does not solve the problem... The issue is that answer is a non-defined variable under Option Explicit.
-3

Don't listen to the professionals. The answer for the ordinary person is: remove statement "option explicit", probably at the top somewhere. Easy, and correct for you and me.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

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.