Меню

Label not defined vba ошибка

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

Asked
7 years, 7 months ago

Viewed
38k 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,4082 gold badges36 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.4k5 gold badges41 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,5711 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

13k6 gold badges56 silver badges106 bronze badges

This is the whole code:

Private Sub CommandButton1_Click()
If ListBox1.Selected(0) Then GoTo errhandler1
ListBox1.Selected(0) = False

If ListBox1.Selected(1) Then GoTo errhandler2
ListBox1.Selected(1) = False

If ListBox1.Selected(2) Then GoTo errhandler3
ListBox1.Selected(2) = False

If ListBox1.Selected(3) Then GoTo errhandler4
ListBox1.Selected(3) = False

If ListBox1.Selected(4) Then GoTo errhandler5
ListBox1.Selected(4) = False

If ListBox1.Selected(5) Then GoTo errhandler6
ListBox1.Selected(5) = False

If ListBox1.Selected(6) Then GoTo errhandler7
ListBox1.Selected(6) = False

errhandler1:
Sheet19.Range(«a7:t45»).PrintOut
Sheet21.Range(«a6:aa42»).PrintOut
Sheet22.Range(«a6:aa69»).PrintOut
Sheet23.Range(«a6:aa27»).PrintOut
Sheet24.Range(«a6:aa66»).PrintOut
Sheet25.Range(«a6:aa21»).PrintOut
Sheet26.Range(«a6:aa39»).PrintOut
Sheet27.Range(«a6:aa54»).PrintOut
Sheet28.Range(«a6:aa66»).PrintOut
Sheet29.Range(«a6:aa36»).PrintOut
Sheet30.Range(«a6:aa81»).PrintOut
Sheet31.Range(«a6:aa27»).PrintOut
End Sub

errhandler2:
Sheet19.Range(«a46:t84»).PrintOut
Sheet21.Range(«a43:aa79»).PrintOut
Sheet22.Range(«a70:aa133»).PrintOut
Sheet23.Range(«a28:aa49»).PrintOut
Sheet24.Range(«a67:aa127»).PrintOut
Sheet25.Range(«a22:aa37»).PrintOut
Sheet26.Range(«a40:aa73»).PrintOut
Sheet27.Range(«a55:aa103»).PrintOut
Sheet28.Range(«a67:aa127»).PrintOut
Sheet29.Range(«a37:aa67»).PrintOut
Sheet30.Range(«a82:aa157»).PrintOut
Sheet31.Range(«a28:aa49»).PrintOut
End Sub

errhandler3:
Sheet19.Range(«a84:t123»).PrintOut
Sheet21.Range(«a80:aa116»).PrintOut
Sheet22.Range(«a134:aa197»).PrintOut
Sheet23.Range(«a50:aa71»).PrintOut
Sheet24.Range(«a128:aa188»).PrintOut
Sheet25.Range(«a38:aa53»).PrintOut
Sheet26.Range(«a74:aa107»).PrintOut
Sheet27.Range(«a104:aa152»).PrintOut
Sheet28.Range(«a128:aa188»).PrintOut
Sheet29.Range(«a68:aa98»).PrintOut
Sheet30.Range(«a158:aa233»).PrintOut
Sheet31.Range(«a50:aa71»).PrintOut
End Sub

errhandler4:
Sheet19.Range(«a124:t162»).PrintOut
Sheet21.Range(«a117:aa153»).PrintOut
Sheet22.Range(«a198:aa261»).PrintOut
Sheet23.Range(«a72:aa93»).PrintOut
Sheet24.Range(«a189:aa249»).PrintOut
Sheet25.Range(«a54:aa69»).PrintOut
Sheet26.Range(«a108:aa141»).PrintOut
Sheet27.Range(«a153:aa201»).PrintOut
Sheet28.Range(«a189:aa249»).PrintOut
Sheet29.Range(«a99:aa129»).PrintOut
Sheet30.Range(«a234:aa309»).PrintOut
Sheet31.Range(«a72:aa93»).PrintOut
End Sub

errhandler5:
Sheet19.Range(«a163:t201»).PrintOut
Sheet21.Range(«a154:aa190»).PrintOut
Sheet22.Range(«a262:aa325»).PrintOut
Sheet23.Range(«a94:aa115»).PrintOut
Sheet24.Range(«a250:aa310»).PrintOut
Sheet25.Range(«a70:aa85»).PrintOut
Sheet26.Range(«a142:aa175»).PrintOut
Sheet27.Range(«a202:aa250»).PrintOut
Sheet28.Range(«a250:aa310»).PrintOut
Sheet29.Range(«a130:aa160»).PrintOut
Sheet30.Range(«a310:aa385»).PrintOut
Sheet31.Range(«a94:aa115»).PrintOut
End Sub

errhandler6:
Sheet19.Range(«a202:t240»).PrintOut
Sheet21.Range(«a191:aa227»).PrintOut
Sheet22.Range(«a326:aa389»).PrintOut
Sheet23.Range(«a116:aa137»).PrintOut
Sheet24.Range(«a311:aa371»).PrintOut
Sheet25.Range(«a86:aa101»).PrintOut
Sheet26.Range(«a176:aa209»).PrintOut
Sheet27.Range(«a251:aa299»).PrintOut
Sheet28.Range(«a311:aa371»).PrintOut
Sheet29.Range(«a161:aa191»).PrintOut
Sheet30.Range(«a386:aa461»).PrintOut
Sheet31.Range(«a116:aa137»).PrintOut
End Sub

errhandler7:
Sheet19.Range(«a241:t279»).PrintOut
Sheet21.Range(«a228:aa264»).PrintOut
Sheet22.Range(«a390:aa453»).PrintOut
Sheet23.Range(«a138:aa159»).PrintOut
Sheet24.Range(«a372:aa432»).PrintOut
Sheet25.Range(«a102:aa117»).PrintOut
Sheet26.Range(«a210:aa243»).PrintOut
Sheet27.Range(«a300:aa348»).PrintOut
Sheet28.Range(«a372:aa432»).PrintOut
Sheet29.Range(«a192:aa222»).PrintOut
Sheet30.Range(«a462:aa537»).PrintOut
Sheet31.Range(«a138:aa159»).PrintOut
End Sub

End Sub

  1. Apr 24th, 2006, 07:15 PM


    #1

    kuuldude is offline

    Thread Starter


    New Member


    Angry Label not defined error for a GoTo Statement

    How do I solve a «Label not defined» for a goto Statement like below

    These are the exact lines

    Public Sub txtSell_KeyPress(KeyAscii As Integer)

    If KeyAscii = vbKeyReturn Then
    GoTo Trick1
    End If
    End Sub

    Hence Trick1: would be located in another part of the code.

    I’d really appreciate some help.


  2. Apr 24th, 2006, 07:20 PM


    #2

    Re: Label not defined error for a GoTo Statement

    Welcome to the forums

    VB Code:

    1. Public Sub txtSell_KeyPress(KeyAscii As Integer)

    2. If KeyAscii = vbKeyReturn Then

    3. GoTo Trick1

    4. End If

    5. 'Maybe more code etc....

    6. Trick1:

    7. End Sub


  3. Apr 24th, 2006, 07:24 PM


    #3

    Re: Label not defined error for a GoTo Statement

    A GoTo statement can only move to something within the current Sub/Function. Perhaps you want to call a different Sub?

    VB Code:

    1. Public Sub txtSell_KeyPress(KeyAscii As Integer)

    2.     If KeyAscii = vbKeyReturn Then

    3.         Trick1

    4.     End If

    5. End Sub

    6. Private Sub Trick1()

    7.    ' Some Code

    8. End Sub


  4. Apr 24th, 2006, 07:24 PM


    #4

    Re: Label not defined error for a GoTo Statement

    Trick1 has to be in the same procedure as the GoTo, but using GoTo like that is usually not a good idea.


  5. Apr 24th, 2006, 07:30 PM


    #5

    kuuldude is offline

    Thread Starter


    New Member


    Re: Label not defined error for a GoTo Statement

    Then, how do I send it to a line that is not within the same sub process.

    What statement can I use?


  6. Apr 24th, 2006, 07:32 PM


    #6

    Re: Label not defined error for a GoTo Statement

    Use bushmobiles example if you want to call another sub routine from your current one.


  7. Apr 24th, 2006, 07:33 PM


    #7

    Re: Label not defined error for a GoTo Statement

    Your code shouldn’t be designed so that you need to jump into the middle of another sub.


  8. Apr 24th, 2006, 07:35 PM


    #8

    kuuldude is offline

    Thread Starter


    New Member


    Re: Label not defined error for a GoTo Statement

    Some example code would be nice!


  9. Apr 24th, 2006, 07:36 PM


    #9

    Re: Label not defined error for a GoTo Statement


  1. 07-24-2018, 02:51 AM


    #1

    DhanyaTeacher is offline


    Registered User


    Very urgent — Getting error Label not defined even after defining it.

    Hi,

    When I tried to compile my VBA code I’m getting an error that says «Label not defined «. The label that it is referring to is getNextRowStudentVIIandVIII.
    I’m attaching the excel with the macro and vba code. Kindly help me. Its quite urgent. Thanks friends.


  2. 07-24-2018, 03:54 AM


    #2

    Re: Very urgent — Getting error Label not defined even after defining it.

    If this is a label as in

    then the label needs a colon ( : ) on the end of it.

    Trevor Shuttleworth — Excel Aid

    I dream of a better world where chickens can cross the road without having their motives questioned

    ‘Being unapologetic means never having to say you’re sorry’ John Cooper Clarke


  3. 07-24-2018, 06:08 AM


    #3

    Re: Very urgent — Getting error Label not defined even after defining it.

    Hi !

    As a good code does not need any label — except for error handling — since more than 20 years for VBA ! …


  4. 07-24-2018, 06:55 AM


    #4

    DhanyaTeacher is offline


    Registered User


    Re: Very urgent — Getting error Label not defined even after defining it.

    Hi , I have given a colon. I rechecked. Still getting the same error


  5. 07-24-2018, 07:10 AM


    #5

    Re: Very urgent — Getting error Label not defined even after defining it.

    In this case, it is because you have put an «End Sub» after each of the Return statements. You will need to comment them out except for the last, the actual end of the subroutine. It will then compile. GoSub is not an approach that I use so I cannot comment on its effectiveness.


  6. 07-24-2018, 07:17 AM


    #6

    Re: Very urgent — Getting error Label not defined even after defining it.

    Gosub and Goto are statements from more than 30 years ago of the VBA grand pa’ : the BASIC !
    But they were left with the coming of some advanced version like Quick Basic and Turbo Basic more than 20 years ago.
    So since the beginning of VBA (more than 20 years) a good code does not need any Gosub neither any Goto statement …


  7. 07-24-2018, 07:36 AM


    #7

    Re: Very urgent — Getting error Label not defined even after defining it.

    @Marc L: I agree with you, but the OP is where the OP is, so it is perhaps more helpful to get the code to compile and, hopefully, work rather than criticising the approach. I’d probably use called subroutines or functions but, that said, I wouldn’t know where to start to address this problem … so the OP is one or two steps up on me.


  8. 07-25-2018, 06:56 AM


    #8

    DhanyaTeacher is offline


    Registered User


    Re: Very urgent — Getting error Label not defined even after defining it.

    Thanks a lot friends. So many end subs !! I agree. It is compiling now. Thank you very much


  9. 07-25-2018, 08:37 AM


    #9

    Re: Very urgent — Getting error Label not defined even after defining it.

    You’re welcome.


0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Laserjet pro m402dn ошибка памяти расходных материалов
  • L3100 epson критическая ошибка