+ 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 Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Win 11)
    Posts
    3,378

    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

  2. #2
    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!

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