How to treat an empty user input box in if statement

Raddle

New Member
Joined
Oct 24, 2023
Messages
37
Office Version
  1. 2016
Hi

I have a sub that asks the user for the number of files they want.
However if user just hits ok the routine should exit gracefully.
Right now if they hit ok with nothing entered, the routine carries on and does not exit.

I know this is basic and I have looked it up and found tonnes on null / is nothing / = 0 etc but it would seem the code does not recognise the empty box as I need it to.

I have to got this right now

I = InputBox("Please enter the required amount", "Enter Amount")
Debug.Print I

If I is nothing Then

Exit Sub
Else
'rest of my routine

Any hints welcome
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Have you tried

if I = "" then
Hi =- thank you.

Yes I have and if the user hits space bar this exits as it should.

But more likely, they will just hit OK which does still continue.

Grrrr

I think the question is how to tell it, nothing is literally nothing ...
 
Upvote 0
So weirdly, if I change it to this, it will exit if I just hit ok but it also breaks it, for when I enter say 5.

I = InputBox("Please enter the required amount", "Enter Amount")
Debug.Print I

If I Is Nothing Then

Exit Sub
Else
 
Upvote 0
True candy that is ...

I = InputBox("Please enter the required amount", "Enter Amount")
Debug.Print I

If Len(I) = 0 Then

Exit Sub
Else


Thank you so much ;)
 
Upvote 0
Try this:
VBA Code:
I = InputBox("Please enter the required amount", "Enter Amount")

If Len(Trim(I)) = 0 Then
    MsgBox "Nothing selected"
    Exit Sub
End If
This will treat a single space like a blank entry.
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,995
Members
449,094
Latest member
masterms

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