Cancel/Exit Sub

  • Thread starter Thread starter Howard
  • Start date Start date
H

Howard

This code works but, how can I exit this code if I hit "Cancel" or "OK?"
Field (column) 7 has either a yes or a no in the cell.

With ActiveSheet
.UsedRange.AutoFilter Field:=7, Criteria1:=InputBox(prompt:="Enter
Yes or No")
.PageSetup.Orientation = xlLandscape
.PrintOut
.UsedRange.AutoFilter
End With
Range("A1").Select

Thanks,
 
myFilter = InputBox(prompt:="Enter Yes or No")
If myFilter = "" Then
'User did not enter anything or hit cancel
msgbox "Please enter Yes or No",vbinformation
exit sub
End If

With ActiveSheet
.UsedRange.AutoFilter Field:=7, Criteria1:=myFilter
.PageSetup.Orientation = xlLandscape
 
myFilter = InputBox(prompt:="Enter Yes or No")

In this case it would be better to use a MsgBox.

Dim Res As VbMsgBoxResult
Res = MsgBox("Click yes or no.")
If Res = vbYes Then
' user clicked Yes
Else
' user clicked No
End If

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
Excellent! Thanks very much.
--
Howard


Brotha Lee said:
myFilter = InputBox(prompt:="Enter Yes or No")
If myFilter = "" Then
'User did not enter anything or hit cancel
msgbox "Please enter Yes or No",vbinformation
exit sub
End If

With ActiveSheet
.UsedRange.AutoFilter Field:=7, Criteria1:=myFilter
.PageSetup.Orientation = xlLandscape
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top