+ Reply to Thread
Results 1 to 15 of 15

Error if cancel or close input box. I want to exit sub.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117

    Question Error if cancel or close input box. I want to exit sub.

    I am a completed vba newbie so any help or advice is appreciated!

    I have some code that goes; users double click cell > input box appears asking for serial number > serial number is found on database sheet and cell that was double clicked is copied over. However, if the user clicks cancel or [x] I get a runtime error (I guess because there is no serial number to find in the database). I want to Exit Sub if the user clicks cancel or [x] or if the user leaves the input box blank and presses okay I want a message box to say "invalid entry." But I'm not sure how to incorporate this into my existing code;


    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
        If 
    Not Intersect(TargetRange("I11:I2010")) Is Nothing Then
            
    If ActiveCell.Value ChrW(&H2713Then
                ActiveCell
    .ClearContents
                Dim SerialNo 
    As Integer
                SerialNo 
    InputBox("Please enter Serial #:")
                
    With Sheets("Lost Property Log")
                .
    Unprotect
                fRow 
    Application.Match(SerialNo, .Columns(3), 0)
                If 
    Not IsError(fRowThen
                    
    .Cells(fRow9).Value Cells(Target.Row9).Value
                End 
    If
                .
    Protect
            End With
            
    Else
            
    SerialNo InputBox("Please enter Serial #:")
            
    ActiveCell.Value ChrW(&H2713)
            
    End If
            
    Cancel True
            With Sheets
    ("Lost Property Log")
                .
    Unprotect
                fRow 
    Application.Match(SerialNo, .Columns(3), 0)
                If 
    Not IsError(fRowThen
                    
    .Cells(fRow9).Value Cells(Target.Row9).Value
                End 
    If
                .
    Protect
            End With
        End 
    If
    End Sub 
    Anyone have any ideas?

    I have attached my workbook so you can see what I'm referring to.

    Thank you!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Error if cancel or close input box. I want to exit sub.


    So the variable SerialNo must be defined as Variant then you could use If SerialNo = "" Then Exit Sub

  3. #3
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117

    Re: Error if cancel or close input box. I want to exit sub.

    Quote Originally Posted by Marc L View Post

    So the variable SerialNo must be defined as Variant then you could use If SerialNo = "" Then Exit Sub
    Thanks for that! I managed to corporate that in and it works. Although, I've realised it might be better if the code said something like if blank or inputted number is not found then message box and then end sub. Any ideas on how I'd do that?

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,046

    Re: Error if cancel or close input box. I want to exit sub.

    If you use the application input box then you could test for a cancel or X click as follows:
    Sub Test()
    i = Application.InputBox("Enter number")
    if i = False then
       MsgBox "User cancelled"
    elseif len(i) = 0 then
       Msgbox "Nothing entered"
    else
       MsgBox "Something entered"
    End If
    End Sub
    If you don't want to use the application inputbox you can achieve the same as follows:
    Sub Test2()
    i = InputBox("Enter number")
    if (StrPtr(i) = 0&) then
       MsgBox "User cancelled"
    elseif len(i) = 0 then
       Msgbox "Nothing entered"
    else
       MsgBox "Something entered"
    End If
    End Sub

  5. #5
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117

    Re: Error if cancel or close input box. I want to exit sub.

    Thanks ByteMarks! Definitely what has to be done but I've managed to make a complete mess of it when incorporating it into my existing code;

    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
        If 
    Not Intersect(TargetRange("I11:I2010")) Is Nothing Then
            
            
            
    'When item is not marked as collected
            If ActiveCell.Value = ChrW(&H2713) Then
                
               SerialNo = Application.InputBox("Please enter Serial #:")
               If SerialNo = False Then
               MsgBox "Please enter Serial # to mark this item as not collected"
               ElseIf Len(i) = 0 Then
               MsgBox "Invalid Serial #"
               Else
               With Sheets("Lost Property Log")
               .Unprotect
               fRow = Application.Match(SerialNo, .Columns(3), 0)
               If Not IsError(fRow) Then
               .Cells(fRow, 9).Value = Cells(Target.Row, 9).Value
                ActiveCell.ClearContents
                .Protect
                End With
                End If
                
                
                

             '
    When item is marked as collected
               
    Else
               
    SerialNo Application.InputBox("Please enter Serial #:")
               If 
    SerialNo False Then
               MsgBox 
    "Please enter Serial # to mark this item as collected"
               
    ElseIf Len(i) = 0 Then
               MsgBox 
    "Invalid Serial #"
               
    Else
               
    With Sheets("Lost Property Log")
                .
    Unprotect
                fRow 
    Application.Match(SerialNo, .Columns(3), 0)
                If 
    Not IsError(fRowThen
                
    .Cells(fRow9).Value Cells(Target.Row9).Value
                ActiveCell
    .Value ChrW(&H2713)
                .
    Protect
                End With
                End 
    If
            
            
            
            
        
    End If 

    So what I was hoping the code says is:

    User double clicks cell >

    1. If the cell is currently blank then serial number box appears and user enters serial number (if user clicks cancel or x or leaves blank then appropriate message appears and sub is ended) > serial number is found on LostPropertyLog and cell on LostPropertyLog and active cell get ticked.

    2. If the cell has a tick already then serial number box appears and user enters serial number (if user clicks cancel or x or leaves blank then appropriate message appears and sub is ended) > serial number is found on LostPropertyLog and cell on LostPropertyLog and active cell get cleared.

    But I'm getting 'End if without Block If' errors and I can't work out where I'm missing my Ifs/End Ifs.


    I also need to add a bit that says if the serial number entered is not found on the LostPropertyLog then it is invalid, a tick is neither added or cleared and a message box appears.

    Can you help? Thank you!

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,046

    Re: Error if cancel or close input box. I want to exit sub.

    Hi I'm a bit confused as to which cells should be ticked/unticked.

    For instance, in the log sheet, row 11 has a serial number of 006 and row 15 has a serial number of 0064.
    If i double-click row 11 and enter 0064 which row should have the tick be assessed on?

    You say the active cell so, based on that, row 11 would be ticked/unticked as a result of finding 0064 or row 15?

  7. #7
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,046

    Re: Error if cancel or close input box. I want to exit sub.

    Hi think this should help. You can change which cell gets ticked

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim SerialNo As String 'declare as string
    If Not Intersect(Target, Range("I11:I2010")) Is Nothing Then
        'with application inputbox, use could use a type of 1 to ensure a number
        'but this would overflow if a number over 32768 was entered.
        SerialNo = Application.InputBox("Please enter Serial")
        If SerialNo = False Then
            'user cancelled
        ElseIf Not IsNumeric(SerialNo) Or SerialNo > 32768 Then
            'invaid number
            MsgBox "Enter a valid number"
        Else
            With Sheets("Lost Property Log")
                'search log
                .Unprotect
                'change the serialNo to integer for the match
                fRow = Application.Match(CInt(SerialNo), .Columns(3), 0)
                If Not IsError(fRow) Then
                    'add the value
                    .Cells(fRow, 9).Value = Cells(Target.Row, 9).Value
                    'set the tick
                    If ActiveCell.Value = ChrW(&H2713) Then
                        ActiveCell = ""
                    Else
                        ActiveCell.Value = ChrW(&H2713)
                    End If
                Else
                    'not found in log
                    MsgBox "Serial # not found in log"
                End If
                .Protect
            End With
        End If
    End If
    End Sub

  8. #8
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117

    Re: Error if cancel or close input box. I want to exit sub.

    Sorry it's confusing! When the user clicks an item on the 'Wanted Items' sheet a link needs to be made with an item on the Lost Property Log.

    So. The user double clicks whatever row in the 'Wanted Items' sheet (in the 'Collected' column) to add a tick and mark it as collected.
    Screen Shot 2018-07-23 at 12.07.46.png
    Then, the input box appears asking the user to attach the row to a pre-existing serial number. In this image I've typed in the serial no '0006'.
    Screen Shot 2018-07-23 at 12.12.47.png
    This will then find the row with the serial no '0006' on the 'Lost Property Log' sheet and mark it's own 'Collected' column with a tick.
    Screen Shot 2018-07-23 at 12.15.59.png
    AS WELL AS marking the row back on the 'Wanted Items' sheet with a tick.
    Screen Shot 2018-07-23 at 12.16.55.png

    And the same process happens when it comes to unticking an item.

    Does that make more sense?

    I've managed to fix all the If/End If errors in the following code but now the find serial no and mark it feature isn't working;

    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
        If 
    Not Intersect(TargetRange("I11:I2010")) Is Nothing Then
        
        
            
    If ActiveCell.Value ChrW(&H2713Then
                
                SerialNo 
    InputBox("Please enter Serial #:")
                If (
    StrPtr(SerialNo) = 0&) Then
                MsgBox 
    "Please enter Serial # to unmark this item as collected"
                
    ElseIf Len(SerialNo) = 0 Then
                MsgBox 
    "Invalid Serial #"
                
    Else
                
    ActiveCell.ClearContents
                Cancel 
    True
                With Sheets
    ("Lost Property Log")
                .
    Unprotect
                fRow 
    Application.Match(SerialNo, .Columns(3), 0)
                If 
    Not IsError(fRowThen
                
    .Cells(fRow9).Value Cells(Target.Row9).Value
                End 
    If
                .
    Protect
                End With
                End 
    If
            
            
            Else
            
                
    SerialNo InputBox("Please enter Serial #:")
                If (
    StrPtr(SerialNo) = 0&) Then
                MsgBox 
    "Please enter Serial # to unmark this item as collected"
                
    ElseIf Len(SerialNo) = 0 Then
                MsgBox 
    "Invalid Serial #"
                
    Else
                
    ActiveCell.Value ChrW(&H2713)
                
    With Sheets("Lost Property Log")
                .
    Unprotect
                fRow 
    Application.Match(SerialNo, .Columns(3), 0)
                If 
    Not IsError(fRowThen
                
    .Cells(fRow9).Value Cells(Target.Row9).Value
                End 
    If
                .
    Protect
                End With
                End 
    If
            
            
    End If
            
    End If

    End Sub 

  9. #9
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117

    Re: Error if cancel or close input box. I want to exit sub.

    Thank you!! I've tweaked it (needed to add/remove the ticks before copying the row) and it does exactly what I want. However, when you leave the input box blank and press OK I get a type mismatch error for the line 'If SerialNo = False Then.' Can you help please? Should be a simple fix but I can't work it out.

    PHP Code: 
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As RangeCancel As Boolean)
    Dim SerialNo As String 'declare as string
    If Not Intersect(Target, Range("I11:I2010")) Is Nothing Then
        '
    with application inputbox, use could use a type of 1 to ensure a number
        
    'but this would overflow if a number over 32768 was entered.
        SerialNo = Application.InputBox("Please enter Serial #:", "Please enter Serial #:")
        If SerialNo = False Then
            '
    user cancelled
            MsgBox 
    "Please enter a valid Serial #"
        
    ElseIf Not IsNumeric(SerialNo) Or SerialNo 9999 Then
            
    'invaid number
            MsgBox "Please enter a valid Serial #"
        Else
            With Sheets("Lost Property Log")
                '
    search log
                
    .Unprotect
                
    'change the serialNo to integer for the match
                fRow = Application.Match(CInt(SerialNo), .Columns(3), 0)
                If Not IsError(fRow) Then
                    '
    add the value
                    
    If ActiveCell.Value ChrW(&H2713Then
                        ActiveCell 
    ""
                        
    .Cells(fRow9).Value Cells(Target.Row9).Value
                    
    Else
                        
    ActiveCell.Value ChrW(&H2713)
                    .
    Cells(fRow9).Value Cells(Target.Row9).Value
                    End 
    If
                Else
                    
    'not found in log
                    MsgBox "Please enter a valid Serial #"
                End If
                .Protect
            End With
        End If
    End If
    End Sub 

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

    Re: Error if cancel or close input box. I want to exit sub.

    Since
    Dim SerialNo As String 'declare as string
    It should be
        If SerialNo = "False" Then

  11. #11
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117

    Re: Error if cancel or close input box. I want to exit sub.

    Thanks! That solves that line. But then I get a mismatch error for the line 'ElseIf Not IsNumeric(SerialNo) Or SerialNo > 9999 Then'

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

    Re: Error if cancel or close input box. I want to exit sub.

    You can not compare string vs numerical
        ElseIf IsNumeric(SerialNo) Then
            If SerialNo > 9999 Then
                'invaid number
                MsgBox "Please enter a valid Serial #"
            End If
        ElseIf Not IsNumeric(SerialNo) Then
            MsgBox "Please enter a valid Serial #"
        Else

  13. #13
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117
    Quote Originally Posted by jindon View Post
    You can not compare string vs numerical
        ElseIf IsNumeric(SerialNo) Then
            If SerialNo > 9999 Then
                'invaid number
                MsgBox "Please enter a valid Serial #"
            End If
        ElseIf Not IsNumeric(SerialNo) Then
            MsgBox "Please enter a valid Serial #"
        Else
    Thanks! That makes sense to me and fixes all those errors but now the rest of the sub doesn’t complete as it should if the number is valid

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

    Re: Error if cancel or close input box. I want to exit sub.

    Other way around
        ElseIf Not IsNumeric(SerialNo) Then
            MsgBox "Please enter a valid Serial #"
        ElseIf IsNumeric(SerialNo) Then
            If SerialNo > 9999 Then
                'invaid number
                MsgBox "Please enter a valid Serial #"
            Else
                With Sheets("Lost Property Log")
                '
                '
                End With
            End If
        End If

  15. #15
    Forum Contributor
    Join Date
    07-21-2018
    Location
    London, England
    MS-Off Ver
    Excel for Mac Version 16.15
    Posts
    117

    Re: Error if cancel or close input box. I want to exit sub.

    Thank you so much! All working 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. [SOLVED] Exit Sub when Cancel is clicked on Input Box
    By rtcwlomax in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2015, 01:57 PM
  2. Replies: 2
    Last Post: 11-28-2014, 07:09 AM
  3. [SOLVED] If Cancel on Input Box, exit sub
    By guitarsweety in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-28-2014, 07:24 PM
  4. Cancel out of Input box for error handling for dates
    By hermithead in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2013, 05:43 AM
  5. [SOLVED] Input Box - Exit Sub on Cancel
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2013, 11:32 AM
  6. Close Input Box and do not filter when cancel selected
    By jiminic in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-01-2008, 11:19 PM
  7. [SOLVED] Input box cancel produces error
    By pkeegs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-12-2006, 01:10 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