I have a code that inserts text into a newly inserted row after it asks for the user to input text into an InputBox. The issue I have is that if the user selects cancel on the InputBox, the code will still run and it will insert a new row and write FALSE into column E. If nothing is entered and I press OK, then it will exit the function correctly. However, how do I also make it so that it exits the function when the user presses cancel?
Public Function ContactAdd() As Long
Dim cF As Range
Dim Response As String
With Worksheets("Database").Range("B:B")
'Find the cell in column B containing value in combobox
Set cF = .Find(What:=Worksheets("Database").ComboBox1, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
'If cell in column B is found
If Not cF Is Nothing Then
'Open inputbox
Response = Application.InputBox("Enter contact name")
'If inputbox is cancelled, exit function
If Response = "" Then
MsgBox "Invalid - Exiting"
Exit Function
Else
'If entry to inputbox is made, insert row below the combobox value that was found and paste response into column E of the row that was inserted
cF.Offset(1, 0).EntireRow.Insert
ContactAdd = cF.Offset(1, 0).Row
Worksheets("Database").Cells(ContactAdd, 5).Value = Response
End If
Exit Function
End If
End With
ContactAdd = 0
End Function
Private Sub InsertContact1_Click()
'Execute ContactAdd function when button is clicked
ContactAdd
End Sub
This part of my code is therefore incorrect and I'm not sure why... I've tried using vbNullString instead of "" as well which didn't help.
If Response = "" Then
MsgBox "Invalid - Exiting"
Exit Function