Search code examples
vbaexcelgoto

VBA "Compile Error: Label not defined"


I have a VBA macro which gave me that error message.

Sub Function1()
    '   Give the user macro options based on how fast or slow the computer 
    '   is using advanced conditional compiling
    vuserChoice = MsgBox("This macro by default treats all numbers as decimals for maximum precision. If you are running this macro on an old computer, you may want to declare numbers as singles, to speed up the macro.")
    MsgBox ("Decimal: recommended for maximum precision. Also slower." & vbNewLine & "Long: not recommended. Rounds to nearest integer." & vbNewLine & "Single: not recommended. A lightweight double." & vbNewLine & "Integer: not recommended. Quick and low-precision.")

    If vuserChoice = "Decimal" Or "decimal" Then
        GoTo FunctionDecimal
    ElseIf vuserChoice = "Double" Or "double" Then
        GoTo FunctionDouble
    ElseIf vuserChoice = "Single" Or "single" Then
        GoTo FunctionSingle
    ElseIf vuserChoice = "Long" Or "long" Then
        GoTo FunctionLong
    Else
        GoTo FunctionNotValidVarType
    End If

    '   MEeff = measure of efflux due to crudely purified HDL in scintillation
    MsgBox "For additional information about this macro:" & vbNewLine & "1. Go to tab Developer" & vbNewLine & "2. Select Visual Basic or Macro." & vbNewLine & "See the comments or MsgBoxes (message boxes)."
End Sub

The offending line is:

GoTo FunctionNotValidVarType

I have the function FunctionNotValidVarType below this code. I have it as:

Public Sub FunctionNotValidVarType()
    MsgBox "VarType " & VarType & " is not supported. Please check spelling."
End Sub

What do I need to do to let the first function recognize FunctionNotValidVarType? Thanks.


Solution

  • GoTo will try and transfer the code execution to a different position in the current Subroutine with the given label.

    Specifically, GoTo FunctionNotValidVarType will try and execute the line:

    FunctionNotValidVarType:  'Do stuff here
    

    which doesn't exist in your current code.

    If you want to call another function use Call FunctionNotValidVarType