how to exit sub if cancel selected or nothing entered inputbox

shahdelsol

Active Member
Joined
Jul 21, 2009
Messages
276
Office Version
  1. 365
Platform
  1. Windows
I am using this vba and I want to add one line of code if "cancel" selected or if "nothing "entered then exit the sub. I tried to add this
If Inputbox (msg) = ""
exit sub
end if
But it doesn't work quite well. I appreciate anyone can help.

VBA Code:
Dim MonEntry As Integer
 Dim Msg As String
 Const MinMonth As Integer = 1
 Const MaxMonth As Integer = 12
 Msg = "What month are your creating a new tab for?"
 Msg = Msg & vbNewLine
 Msg = Msg & "Please enter number for the month between " & MinMonth & " and " & MaxMonth
 
  Do
 
    MonEntry = InputBox(Msg)
    If IsNumeric(MonEntry) Then
     If MonEntry >= MinMonth And MonEntry <= MaxMonth Then Exit Do
     End If
     Msg = "wrong entry"
     Msg = Msg & vbNewLine
     Msg = Msg & "Please enter a valid number for the month!"
     Msg = Msg & vbNewLine
     Msg = Msg & "The number for the month must be between " & MinMonth & " and " & MaxMonth
     
     Loop
     
     Worksheets("TMP").Copy after:=Sheets(Worksheets.Count)

      ActiveSheet.Name = Format(28 * MonEntry, "mmm ") & Year(Now)
    End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Change the declaration of MonEntry to Variant and add this.
VBA Code:
If MonEntry = "" Then
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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