VBA - Run Time 91 error

jbellon19

New Member
Joined
Oct 19, 2017
Messages
5
Hello All,

Posted is my code:

______________________________________________________________________

Private Sub UserForm_Initialize()


key = InputBox("Which Line is this for?", "Line # ?, please enter ONLY a number")

If key = "1" Then
ElseIf key = "2" Then
ElseIf key = "3" Then
ElseIf key = "4" Then
ElseIf key = "5" Then
ElseIf key = "6" Then
ElseIf key = "7" Then
ElseIf key = "8" Then
ElseIf key = "9" Then
ElseIf key = "10" Then
ElseIf key = "11" Then
ElseIf key = "12" Then
Else
Unload Me
MsgBox ("You must enter a number, as indicated")
Exit Sub
End If

End Sub

________________________________________________________________

I have this running as a userform initializes so that I can capture a variable that I use to inject information back into the document (in more than one place).

For the life of me, I can not figure out what I have wrong. Any help would be appreciated!

Thank you
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Exit Sub should come after End If. The code is stopping before finishing the If statement, which more than likely is causing the error.
 
Upvote 0
Exit Sub should come after End If. The code is stopping before finishing the If statement, which more than likely is causing the error.
That is not true at all.
It is quite common to do it this way. Sometimes you want the code to exit the sub in the True or False part of an If statement.
It is perfectly fine to structure the code that way.

Which line of code is returning the error?
 
Upvote 0
How/where have you declared key?


Good Morning!

______________________
Option Explicit


Public key As String
Public product As String
______________________

Those 2 variables have been declared at the top of the userform, prior to any Subs
 
Upvote 0
That is not true at all.
It is quite common to do it this way. Sometimes you want the code to exit the sub in the True or False part of an If statement.
It is perfectly fine to structure the code that way.

Which line of code is returning the error?

It actually doesn't specify a line, and it doesn't open up debug mode. It seems to operate as it should, but then just pushes a msgbox to me that says:

"Run-time error '91':
Object variable or With block variable not set"
 
Upvote 0
I'm pretty sure that you can't have
Code:
Unload Me
In an Initialize sub.
As the form has not been loaded, you can't unload it
 
Upvote 0
I'm pretty sure that you can't have
Code:
Unload Me
In an Initialize sub.
As the form has not been loaded, you can't unload it

Well, that makes sense!

Is there anyway for me to prevent the form to load then if the condition is not met?
 
Upvote 0
Take a look here: https://www.ozgrid.com/forum/forum/...oad-userform-in-the-initialize-event?t=150698

Also, you can use a Case statement instead of a real messy IF statement, i.e.
Code:
    Select Case key
        Case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
            'Do stuff here
        Case Else
            'Do stuff here
    End Select

Perfect, thank you Joe4!!

This makes sense, and I love the idea of a boolean value, because I am going to have to do this a lot.

Yes, using Case makes much more sense, but for beginners I feel that it is a bit harder to understand. I guess I can do it both ways within the code so that readers can begin get comfortable with it.

Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,601
Members
452,658
Latest member
GStorm

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