Label not defined vba ошибка

Asked
7 years, 11 months ago

Viewed
39k times

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's user avatar

asked Jun 24, 2015 at 15:05

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

answered Jun 24, 2015 at 15:09

kaybee99's user avatar

kaybee99kaybee99

4,5282 gold badges32 silver badges42 bronze badges

6

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

answered Jun 24, 2015 at 15:14

SierraOscar's user avatar

SierraOscarSierraOscar

17.5k6 gold badges40 silver badges68 bronze badges

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's user avatar

answered Jun 24, 2015 at 15:10

FreeMan's user avatar

FreeManFreeMan

5,6201 gold badge27 silver badges53 bronze badges

3

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.

answered Jun 24, 2015 at 16:29

Uri Goren's user avatar

Uri GorenUri Goren

13.2k6 gold badges57 silver badges109 bronze badges

I keep getting this msg

VBA

Compile Error:

Label not defined

Yes, Label starts with a letter.

Label Has No spaces

Label Has Nothing but letters.

Label’s First letter is in column 1   ( per the editor in Access VBA code editor )

Label Does end with a “ : “   at end.  
Not with the extra spaces or quotes.

i Did comment the line out with the Label,  ( only label on the line). On a new line I retyped the same label.

i Did save, do a RefreshAll, did a close of VBA editor, then restarted VBA editor, clicked on reset, then debug got same msg.

i did this between every time i tried a different way.

I used GoTo ErrHandler

then added    On Error GoTo ErrHander

I copied the label from the Goto line, then pasted it at spot I want, with first letter in column 1, added “ 
:   at the end,  so no way I could have misspelled the label.

I also did the same using  57    
as the label, I did put label on line 57 just in case.   as in goto a line number.

Every time I get the same msg.

Only thing I noticed that was different is in     Tools / References I see under Available References the below

EditionUpgradeHelperLib

Location  C:WindowsSystems32 EditionUpgradeHelperLib.dll

Language:  Standard

That is the only reference I do NOT ever recall seeing before. If definitely was no picked before.

I did search net, only info I got came to this, download third party app(s) that is supposed to check all “dll” files , fix them, and remove them if you want to, but that doc had a LONG list of things to do before run program, and hope fixes, not make worse.
and how to reinstall the dll if it turns out you MUST have it.

Was never able to find out what that file is supposed to do.

Never before had this msg, unless I made a typo.

I do have under Available references checked

Visula Basic For Applications

Microsoft Access 16.0 Object Library

OLE Automation

Microsoft Office 16.0 Access database engine Object Library

Microsoft Office 16.0 Object Library

i thank you for any help you can give.

mark J


Mark J

Permalink

Cannot retrieve contributors at this time

title keywords f1_keywords ms.prod ms.assetid ms.date ms.localizationpriority

Label not defined

vblr6.chm1011207

vblr6.chm1011207

office

0420c1d7-d2d3-2df3-2157-63834d3ac0d2

06/08/2017

medium

This error has the following cause and solution:

  • A line label or line number is referred to (for example in a GoTo statement), but doesn’t occur within the scope of the reference.

    The label must be within the procedure that contains the reference. Line labels are visible only in their own procedures.

For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).

[!includeSupport and feedback]

Solution 1

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

Solution 2

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

Solution 3

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 :

Comments

  • 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.

  • You don’t even need the word Call it’s just a throwback from legacy VB code.

  • Yes, but I still use it to make it totally clear I’m calling another function

  • 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

  • Or, @ThomasShera, as S O (and others) pointed out, you can remove both the goto and the call and be just fine.

  • @FreeMan OK, thanks! I have a new error message now. Should I edit the original post?

  • Nope. The SE way is one question, one (accepted) answer. Start up a new question.

  • well, you could use the goto, @cptn_hammer, in an On Error Goto..., but there are few other valid times…

  • 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.

  • Ahh didn’t look at your username :)

Recents

Related

vet2810

0 / 0 / 0

Регистрация: 13.05.2015

Сообщений: 7

1

19.02.2016, 08:01. Показов 7916. Ответов 4

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

Здравствуйте, помогите, пожалуйста, урок из книжки. Пишу код, а он мне выдает Compile error: Label not defined

Visual Basic
1
2
3
4
5
6
7
8
Sub It2()
    Dim X As Integer
    X = 12
2:  If X > 9 And X < 12 Then GoTo LasLine - ошибку выдает в этом месте
    X = X - 12
    GoTo 2
LastLine:
End Sub

Исправить Compile error: Label not defined в коде



0



Shersh

Заблокирован

19.02.2016, 08:20

2

vet2810, LasLine<>LastLine



1



0 / 0 / 0

Регистрация: 13.05.2015

Сообщений: 7

19.02.2016, 08:27

 [ТС]

3

sorry, что не по теме, сразу не нашел подходящую.
Могли бы Вы более подробно описать мою ошибку.
Видите ли я только учусь, выполняю урок из книги, все написал точь в точь как описано, а оно ВОТ



0



Shersh

Заблокирован

19.02.2016, 08:29

4

Цитата
Сообщение от Shersh
Посмотреть сообщение

LasLine<>LastLine

Экранную лупу включите.



1



5590 / 1580 / 406

Регистрация: 23.12.2010

Сообщений: 2,366

Записей в блоге: 1

19.02.2016, 09:12

5

Выдает ошибку, потому что пропущена буква t в названии метки.



0



Понравилась статья? Поделить с друзьями:
  • La noire ошибка при установке
  • La noire ошибка при запуске 0xc0000142
  • La noire ошибка при запуске 0xc000007b
  • La noire ошибка передачи файла
  • La noire ошибка отсутствует dll переустановите social club