InputBox if Cancel

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
How do I get my procedure to exit if cancel is clicked in the InputBox?

Code:
Sub FindTest_Click()

Dim FINDTEST As Variant
Dim TestNameRng As Range
Dim CurrTest As Variant

Set TestNameRng = Range("G7:ProdCodeDesc")
FINDTEST = InputBox(" Enter the name of the test, ***EXACTLY*** as it appears on the data sheet", _
    "DELETE TEST", "Enter TEST NAME Here", 6500, 3000)
            
Application.ScreenUpdating = False

With TestNameRng.Select
    Selection.find(What:=FINDTEST, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Select

Application.ScreenUpdating = True

Set CurrTest = ActiveCell

If MsgBox("Is " & CurrTest & "  the correct test to delete from the data sheet", vbYesNo) = vbYes Then

CurrTest.EntireColumn.Delete

Else
Exit Sub
End If
End With
End Sub

So far I have tried

Code:
Dim FINDTEST As Variant
Dim TestNameRng As Range
Dim CurrTest As Variant

Set TestNameRng = Range("G7:ProdCodeDesc")
If FINDTEST = InputBox(" Enter the name of the test, ***EXACTLY*** as it appears on the data sheet", _
    "DELETE TEST", "Enter TEST NAME Here", 6500, 3000) = 0 Then
Exit Sub
    Else
           
Application.ScreenUpdating = False

With TestNameRng.Select
    Selection.find(What:=FINDTEST, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Select

Application.ScreenUpdating = True

Set CurrTest = ActiveCell

If MsgBox("Is " & CurrTest & "  the correct test to delete from the data sheet", vbYesNo) = vbYes Then

CurrTest.EntireColumn.Delete

Else
Exit Sub
End If
End With
End If

and

Code:
If FINDTEST = InputBox(" Enter the name of the test, ***EXACTLY*** as it appears on the data sheet", _
    "DELETE TEST", "Enter TEST NAME Here", 6500, 3000) = False Then
    Exit Sub
        Else
         
Application.ScreenUpdating = False
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Code:
If FINDTEST = InputBox(" Enter the name of the test, ***EXACTLY*** as it appears on the data sheet", _
    "DELETE TEST", "Enter TEST NAME Here", 6500, 3000) = "" Then Exit Sub

InputBox returns a "" if cancel is entered.
 
Upvote 0
Code:
Sub FindTest_Click()

Dim FINDTEST As [COLOR=#ff0000]String[/COLOR]
Dim TestNameRng As Range
Dim CurrTest As Variant

Set TestNameRng = Range("G7:ProdCodeDesc")
FINDTEST = InputBox(" Enter the name of the test, ***EXACTLY*** as it appears on the data sheet", _
    "DELETE TEST", "Enter TEST NAME Here", 6500, 3000)
[COLOR=#ff0000]If FINDTEST = "" Then Exit Sub[/COLOR]
 
Upvote 0
Code:
If FINDTEST = False Then Exit Sub
 
Upvote 0
Thanks AlphaFrog, that worked perfectly. Thankstlowry for the response, I was still having the same problem with you code. it was continuing on to
Code:
 If MsgBox("Is " & CurrTest & "  the correct test to delete from the data sheet", vbYesNo) = vbYes Then
of the code! Thanks for your help though, it is appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top