2

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.

Michael Coxon
  • 5,311
  • 1
  • 24
  • 51

4 Answers4

4

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

kaybee99
  • 4,566
  • 2
  • 32
  • 42
  • 4
    You don't even need the word `Call` it's just a throwback from legacy VB code. – SierraOscar Jun 24 '15 at 15:14
  • 1
    Yes, but I still use it to make it totally clear I'm calling another function – kaybee99 Jun 24 '15 at 15:19
  • There is a huge difference between calling a subroutine and the `GoTo` statement. after the subroutine we've just call ended, the execution continues from the line we called the `Sub`, where in the `GoTo` scenario, it does not – Uri Goren Jun 24 '15 at 16:43
  • Or, @ThomasShera, as S O (and others) pointed out, you can remove both the `goto` and the `call` and be just fine. – FreeMan Jun 24 '15 at 16:59
  • @FreeMan OK, thanks! I have a new error message now. Should I edit the original post? –  Jun 24 '15 at 17:02
  • Nope. The SE way is one question, one (accepted) answer. Start up a new question. – FreeMan Jun 24 '15 at 17:03
1

Remove Goto from the call to your Sub()

If you really wanted to use a Goto (and you shouldn't), you would

goto Label

Label:

where the label is defined by the trailing colon :

Community
  • 1
  • 1
FreeMan
  • 5,660
  • 1
  • 27
  • 53
  • well, you _could_ use the `goto`, @cptn_hammer, in an `On Error Goto...`, but there are few other valid times... – FreeMan Jun 24 '15 at 17:04
  • I just added the link on shouldn't. I prefer to give people reasons *why* something is the way it is, rather than making sweeping proclamations. Information breeds understanding. – ale10ander Jun 24 '15 at 17:11
  • Ahh didn't look at your username :) – ale10ander Jun 24 '15 at 17:14
1

Remove the word GoTo

GoTo tells the code to jump to a label, you want it to enter a new procedure, not go to a label

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
0

GoTo transitions to a label, a label is defined with :

For example:

Sub G()
On Error GoTo err_handling
  a=1/0
  Exit Sub
err_handling:
  MsgBox "Holy Shit, an error occurred !"
End Sub

To apply GoTo on a Sub you need call it and exit:

Call FunctionNotValidVarType
Exit Sub

(Technically, it is not the same as GoTo if you take the call stack into consideration, but the end result is the same)

GoTo is not considered a good practice, but if that doesn't concern you, take a look also at GoSub at the official docs.

Uri Goren
  • 13,386
  • 6
  • 58
  • 110