+ Reply to Thread
Results 1 to 4 of 4

Error handling on InputBox entry when nothing is entered

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2010
    Location
    Seaton Delaval, Northumberland
    MS-Off Ver
    Office 365
    Posts
    31

    Question Error handling on InputBox entry when nothing is entered

    Hi all,

    I have some code that creates a new sheet and names it what ever the user enters into an InputBox.

    It works when they enter a name.
    It works when they click 'Cancel'.
    But it returns a “Run-time error ‘9’@: Subscript out of range” error if you click OK without entering anything in the InputBox.

    How do I stop it/loop it back after the ‘If Response = "" Then” section because it’s trying to create a sheet without a name!

    I can't get my head around it! I'm trying to teach myself VBA but it's going very slowly.

    Sub AddNewSheet()
    
        Sheets("Template").Visible = True
        Sheets("Template").Copy After:=Worksheets(Worksheets.Count)
        Sheets("Template").Visible = False
        Response = Application.InputBox("Enter the technician's name:")
        
        If Response = False Then
            Application.DisplayAlerts = False
            Sheets("Template (2)").Delete
            Sheets("Main").Select
        End If
        
        If Response = "" Then
            MsgBox "Please enter a technician's name."
            Application.DisplayAlerts = False
            Sheets("Template (2)").Delete
            Sheets("Main").Select
        End If
               
        If Response <> False Then
            Sheets("Template (2)").Name = Response
        End If
    
    End Sub
    Last edited by Kungfauxn00b; 03-24-2011 at 04:45 AM.
    Dave (Excel n00b)

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Error handling on InputBox entry when nothing is entered

    This is how I'd arrange that...
    Sub AddNewSheet()
        
        Do
            response = Application.InputBox("Enter the technician's name:", _
                "Enter Name", "John Doe", Type:=2)
            If response = False Then Exit Sub
            If response <> "" Then Exit Do
        Loop
        
        Sheets("Template").Visible = True
        Sheets("Template").Copy After:=Worksheets(Worksheets.Count)
        Sheets("Template").Visible = False
        Sheets("Template (2)").Name = response
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Error handling on InputBox entry when nothing is entered

    Hi Kungfauxn00b

    Here's one way
    Sub AddNewSheet()
        Sheets("Template").Visible = True
        Sheets("Template").Copy After:=Worksheets(Worksheets.Count)
        Sheets("Template").Visible = False
        Response = Application.InputBox("Enter the technician's name:")
     
        If Response = False Then
            Application.DisplayAlerts = False
            Sheets("Template (2)").Delete
            Sheets("Main").Select
        End If
     
        If Response = "" Then
            MsgBox "Please enter a technician's name."
            Application.DisplayAlerts = False
            Sheets("Template (2)").Delete
            Sheets("Main").Select
            Exit Sub
        End If
     
        If Response <> False Then
            Sheets("Template (2)").Name = Response
        End If
    End Sub
    Last edited by jaslake; 03-23-2011 at 05:04 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Registered User
    Join Date
    02-09-2010
    Location
    Seaton Delaval, Northumberland
    MS-Off Ver
    Office 365
    Posts
    31

    Re: Error handling on InputBox entry when nothing is entered

    Awesome, thanks guys!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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