+ Reply to Thread
Results 1 to 8 of 8

How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

  1. #1
    Forum Contributor
    Join Date
    11-19-2019
    Location
    Michigan
    MS-Off Ver
    Office 365
    Posts
    548

    How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    Hello, I have this code that has a couple of input boxes. I just noticed a flaw, if I press cancel or exit out, the code still runs. Is there a way to stop the code if user presses cancel on any of the input boxes and stops the code from creating a sheet?
    Here is my code

    Please Login or Register  to view this content.
    Thanks.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,939

    Re: How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    Maybe test: If employeeID = "" Then Exit Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,790

    Re: How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    QueryClose

  4. #4
    Forum Contributor
    Join Date
    11-19-2019
    Location
    Michigan
    MS-Off Ver
    Office 365
    Posts
    548

    Re: How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    Sorry I am not an expert. Can you kindly show me how to use it in my example code above?

  5. #5
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,790

    Re: How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    Not without a sample worksheet but there's always someone willing to do it without one.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    Please Login or Register  to view this content.

  7. #7
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,675

    Re: How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    Quote Originally Posted by wherdzik View Post

    Sheets("Template - Phys Standard").Copy Before:=Sheets(1)
    employeeID = InputBox("Enter Employee ID")
    Because copy first, then input box.
    Try this:
    PHP Code: 
    Sub Add_Physician()
        
    Dim employeeID
        Dim LastName
        employeeID 
    InputBox("Enter Employee ID")
        If 
    Len(employeeID) = 0 Then Exit Sub
        LastName 
    InputBox("Enter Employee Last Name")
        If 
    Len(LastName) = 0 Then Exit Sub
        Sheets
    ("Template - Phys Standard").Copy Before:=Sheets(1)
        
    Sheets(1).Name employeeID "_" LastName
        Range
    ("E7").Value employeeID
    End Sub 
    Quang PT

  8. #8
    Forum Contributor
    Join Date
    11-19-2019
    Location
    Michigan
    MS-Off Ver
    Office 365
    Posts
    548

    Re: How to Stop Macro If User Presses Cancel on Input box or Presses X for Exit

    Oh wow...thank you Jindon and thank you bebo021999...Your solutions are beautiful.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] User Form automatically sends SMS Text Msg when user presses Submit Button
    By BigHungarian in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-19-2021, 12:28 PM
  2. [SOLVED] Want to wait to run macro until all info is filled in and agent presses OK
    By Jamesraywebber85 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-10-2018, 10:20 AM
  3. [SOLVED] How do I keep the Excel UserForm from clearing when user Presses Done
    By eemiller1997 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-26-2012, 10:35 AM
  4. Detecting key presses
    By Jonathan Smith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2006, 03:30 PM
  5. How to restore a Custom menu if a user presses Cancel after selcting exit
    By Alseikhan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-04-2006, 01:25 AM
  6. Simulating Key Presses in Macros
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2006, 05:50 AM
  7. Creating a macro which presses a button containing a recorded macro
    By petros89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-05-2005, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1