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
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 Range, Cancel As Boolean) If Not Intersect(Target, Range("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(fRow) Then .Cells(fRow, 9).Value = Cells(Target.Row, 9).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.
Bookmarks