VBA Compile error Label not defined

bkey01

New Member
Joined
Apr 12, 2004
Messages
25
I have an Excel workbook with multiple listbox's and command button. I have set an errhandler for printing. The first time it worked find and know I am getting an error that says:
Compile error
Label not found
for errhandler7

Ex. code

if listbox1.selected(7) then goto errhandler7
listbox1.selected(7)=false

errhandler7:
sheet1.printout
sheet2.printout
sheet3.range("a1:aa40").print

Can anyone help
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
At first glance that looks fine. Any chance you copy and pasted this same code into another part of the project and your compile error is happening elsewhere?
 
Upvote 0
If the sample code you posted is not your *actual* code, have you checked to make sure you have the label spelled the same way in both places?
 
Upvote 0
Sorry, you've stumped me. I copy and pasted the code you posted into a couple of button click-event handlers, changed the prints to a msgbox and I'm not having any problems.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()
    <SPAN style="color:#00007F">If</SPAN> ListBox1.Selected(7) <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> errhandler7
    ListBox1.Selected(7) = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
errhandler7:
    MsgBox "Lo 7"

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton2_Click()
    <SPAN style="color:#00007F">If</SPAN> ListBox1.Selected(7) <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> errhandler7
    ListBox1.Selected(7) = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
errhandler7:
    MsgBox "Hi 7"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

And button1 one had its TakeFocusOnClick set to TRUE while button2 had it set to FALSE, so that isn't an issue. Sorry wasn't more help. I still suspect a cut and paste or similar error (I've copied more code, knowing I needed to fix a bit here and there and then forgotten to do it...)

Good luck,
 
Upvote 0
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
 
Upvote 0
I would say your problem is that you're using End Sub at all of those. You've effectively made it so that anything after the End Sub at the end of errhandler1 isn't included in the macro.

Put End Sub at the end of your code and EXIT sub in place of the rest and give it a go.
 
Upvote 0
It should have bellyached way before #7. Change your "end sub" statements to "exit sub" statements. {Edit}Blast, I responded as quick I my lil' ol' fingers could go and Kristy still beat me...Hiya Mrs. von Pookie!!!{EndEdit}
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,052
Latest member
Fuddy_Duddy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top