goto subroutine help

depcdivr

Active Member
Joined
Jan 21, 2008
Messages
349
Office Version
  1. 365
Platform
  1. Windows
I have a userform setup such than the user can enter a part number. I am trying to do a check to make sure that a part number is valid. If it is not valid then I want to go to a sub routine where I put a msgbox stating it is an invalid number and then exit the sub. HEre is some of the code that I am running.

Code:
 opt(1) = Mid(ipn, 5, 1)
        If opt(1) <> 1 Or opt(1) <> 2 Or opt(1) <> 3 Or opt(1) <> 4 Then
             inv_opt = 1
             inv_val = opt(1)
             GoTo Invalid_PN_code
        End If
 
        opt(2) = Mid(ipn, 6, 1)
        If opt(2) <> 0 Or opt(2) <> 1 Or opt(2) <> 2 Then
             inv_opt = 2
             inv_val = opt(2)
             GoTo Invalid_PN_code
        End If
 
        opt(3) = Mid(ipn, 7, 1)
        If opt(3) <> 0 Or opt(3) <> 1 Or opt(3) <> 2 Then
             inv_opt = 3
             inv_val = opt(3)
             GoTo Invalid_PN_code
        End If
 
        opt(4) = Mid(ipn, 8, 1)
        If opt(4) <> 0 Or opt(4) <> 1 Or opt(4) <> 2 Then
             inv_opt = 4
             inv_val = opt(4)
             GoTo Invalid_PN_code
 
        End If
 
'more code here
' \/ 
' \/
' \/
 
'ends with this code
 
 
 
Invalid_PN_code:
    MsgBox "Invalid " + Prod + " Part Number" + Chr(10) + "A " + inv_val + " for Option " + inv_opt + "  is not a valid option."
    Exit Sub
 
End Sub

Unfortuantely no matter what I do the invalid_pn_code routine always runs. What am I doing wrong?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try using

Code:
Exit Sub

immediately before

Code:
Invalid_PN_code:

But you should try to avoid GoTos - bad programming practice.
 
Upvote 0
How would you suggest that I do this bit of code without the GoTo?
I want to call the same sub routine if the code fails for any of a dozen of products.
 
Upvote 0
That exit sub worked but now I am getting a type mismatch error # 13?
 
Upvote 0
I figured it out. I needed to change the +'s to &'s then it worked fine.
 
Upvote 0
I should have spotted that - sorry I was concentrating on the original question.

I would still look at whether you can get rid of the GoTos - it is not the end of the world but better to avoid them if possible :)
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,720
Members
449,050
Latest member
MiguekHeka

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