InputBox - Pressing Cancel doesn't exit sub

katekoz

Board Regular
Joined
Jan 20, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
I may be naive, but I expected that the default setting for pressing "Cancel" on a message box would be to exit sub. That's not the case! ? Nothing happens and it just continues on. Is there some code I need to add to get the program to end sub when "Cancel" is pressed?

Here's one of the little pieces of code with an inputbox. I have many of them throughout a few different macros.

VBA Code:
UserID = InputBox("Enter the username you'd like to add training status for.", vbOKCancel)
DateID = InputBox("Enter the first day of the month you'd like to add training for.", vbOKCancel)

ThisWorkbook.Sheets("Training").Range("I2").Value = UserID
ThisWorkbook.Sheets("Training").Range("J2").Value = DateID
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Maybe the Syntax below?
VBA Code:
Dim UserID as String
UserID = InputBox("Enter the username you'd like to add training status for.", vbOKCancel)
If UserID = vbNullString Then Exit Sub
 
Upvote 0
That worked! Simple, thanks! I'll add it throughout the set of macros I'm working on. Have a great day!
 
Upvote 0
Try this

VBA Code:
Sub test()
  Dim UserId, DateID
  UserId = InputBox("Enter the username you'd like to add training status for.", vbOKCancel)
  If StrPtr(UserId) = 0 Then
    MsgBox "The user clicked Cancel, so we will exit the subroutine now."
    Exit Sub
  ElseIf Len(UserId) = 0 Then
    MsgBox "The user clicked OK without entering anything in the InputBox!"
    Exit Sub
  Else
    MsgBox "The user entered the following..." & vbLf & vbLf & UserId
    'continue
  End If


  DateID = InputBox("Enter the first day of the month you'd like to add training for.", vbOKCancel)
  If StrPtr(DateID) = 0 Then
    MsgBox "The user clicked Cancel, so we will exit the subroutine now."
    Exit Sub
  ElseIf Len(DateID) = 0 Then
    MsgBox "The user clicked OK without entering anything in the InputBox!"
    Exit Sub
  Else
    MsgBox "The user entered the following..." & vbLf & vbLf & DateID
    'continue
  End If
  
  ThisWorkbook.Sheets("Training").Range("I2").Value = UserId
  ThisWorkbook.Sheets("Training").Range("J2").Value = DateID
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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