+ Reply to Thread
Results 1 to 3 of 3

Thread: Code error when cancelling

  1. #1
    Registered User
    Join Date
    12-11-2006
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    82

    Code error when cancelling

    Hello everyone

    I am trying to use the code below as a sheet selection mechanism.

    Sub Sheet_Select()
    myShts = ActiveWorkbook.Sheets.Count
    For i = 1 To myShts
    myList = myList & i & " - " & ActiveWorkbook.Sheets(i).Name & " " & vbCr
    Next i
    Dim mySht As Single
    mySht = InputBox("Please enter the number of the transaction you would like to view." _
    & vbCr & vbCr & myList, "Transaction Selection")
    
    If mySht <> "" Then
                Sheets(mySht).Select
                
                Else
                Range("B1").Select
        End If
    
    End Sub
    If I enter a sheet number, the code works perfectly, but if I hit the cancel button I get a type mismatch error message that relates to this part of the code:

    mySht = InputBox("Please enter the number of the transaction you would like to view." _
    & vbCr & vbCr & myList, "Transaction Selection")
    Does anyone have any suggestions on what is wrong with the code I am using?

    Thank you

  2. #2
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998
    InputBox returns a string. And you had mySht dimensioned as Single.
    This changes that and adds a bit more protection incase the user enters a non-numeric value in the InputBox.
    Sub Sheet_Select()
    Dim myShts As Long, myList As String
    Dim i As Long
    
    myShts = ActiveWorkbook.Sheets.Count
    For i = 1 To myShts
        myList = myList & i & " - " & ActiveWorkbook.Sheets(i).Name & " " & vbCr
    Next i
    Dim mySht As String
    mySht = InputBox("Please enter the number of the transaction you would like to view." _
    & vbCr & vbCr & myList, "Transaction Selection")
    
    On Error Resume Next
    If Not (IsNumeric(Sheets(Val(mySht)).Index)) Then
        Range("b1").Select
    Else
        Sheets(Val(mySht)).Activate
    End If
    On Error GoTo 0
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    12-11-2006
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    82
    Thank you so much mikerickson, it works perfectly now.

+ 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. Imported VBA code not working....
    By HuskerBronco in forum Excel Programming
    Replies: 10
    Last Post: 09-02-2008, 05:57 PM
  2. CountA - Dynamic Range - Macro
    By elcentro3m in forum Excel Programming
    Replies: 12
    Last Post: 09-10-2007, 02:54 PM
  3. Placement of a particular line of code
    By smurray444 in forum Excel Programming
    Replies: 5
    Last Post: 08-19-2007, 11:15 AM
  4. Running code while displaying a UserForm with vbModeless
    By PilgrimTim in forum Excel Programming
    Replies: 7
    Last Post: 08-06-2007, 08:08 AM

Tags for this Thread

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.2.0