PC Review


Reply
Thread Tools Rate Thread

Cancelling But Macro Still Carries On

 
 
kmzito@gmail.com
Guest
Posts: n/a
 
      21st May 2009
Good afternoon all,

I have the below macro. I entered an addition line for when the user
presses the cancel box, but the macro keeps going after I press
cancel. I'd like it to stop and go back to the main menu.


SORT_VENDOR

Dim sUsername As String
Dim sPrompt As String

sPrompt = "Please enter vendor name"
sUsername = InputBox(sPrompt, sTitle, sDefault)

Sheets("list").Select
Range("C3").Select
Cells.Find(What:=sUsername, After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Select
If sUsername = "" Then
MsgBox "You cancelled the search."
Sheets("welcome").Select
Range("a1").Select

Exit Sub

Answer = MsgBox("Is this the contract/vendor you would like to
delete?", vbYesNo + vbInformation, "Please Confirm")
If Answer = vbYes Then
Select Case LCase(Range("B" & Selection.Row).Value)

Case "monthly"
Selection.Resize(12, 1).EntireRow.Delete
Case "quarterly"
Selection.Resize(4, 1).EntireRow.Delete
Case "yearly"
Selection.EntireRow.Delete
End Select
Exit Sub

Else
Cells.Find(What:=sUsername, After:=ActiveCell, LookIn:=xlFormulas,
LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
End If

DELETE_EMPTYROWS
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      21st May 2009
The Cancel on the InputBox dialog box does not stop the macro. It only
closes the InputBox without a value. If you want the Cancel button to cause
the macro to stop, then you need to add the following statement.

If sUsername = "" Then
Exit Sub
End If

Howeve, clicking OK with no entry in the InputBox also produces the empty
string "" and will exit the sub as well.

<(E-Mail Removed)> wrote in message
news:79804d2a-11cf-401b-9725-(E-Mail Removed)...
> Good afternoon all,
>
> I have the below macro. I entered an addition line for when the user
> presses the cancel box, but the macro keeps going after I press
> cancel. I'd like it to stop and go back to the main menu.
>
>
> SORT_VENDOR
>
> Dim sUsername As String
> Dim sPrompt As String
>
> sPrompt = "Please enter vendor name"
> sUsername = InputBox(sPrompt, sTitle, sDefault)
>
> Sheets("list").Select
> Range("C3").Select
> Cells.Find(What:=sUsername, After:=ActiveCell, LookIn:=xlFormulas,
> LookAt _
> :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
> MatchCase:= _
> False, SearchFormat:=False).Select
> If sUsername = "" Then
> MsgBox "You cancelled the search."
> Sheets("welcome").Select
> Range("a1").Select
>
> Exit Sub
>
> Answer = MsgBox("Is this the contract/vendor you would like to
> delete?", vbYesNo + vbInformation, "Please Confirm")
> If Answer = vbYes Then
> Select Case LCase(Range("B" & Selection.Row).Value)
>
> Case "monthly"
> Selection.Resize(12, 1).EntireRow.Delete
> Case "quarterly"
> Selection.Resize(4, 1).EntireRow.Delete
> Case "yearly"
> Selection.EntireRow.Delete
> End Select
> Exit Sub
>
> Else
> Cells.Find(What:=sUsername, After:=ActiveCell, LookIn:=xlFormulas,
> LookAt _
> :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
> MatchCase:= _
> False, SearchFormat:=False).Activate
> End If
>
> DELETE_EMPTYROWS



 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      22nd May 2009
Why do you have vbInformation? It is not needed


vbYesNo = 4
vbInformation = 64
answer = vbYesNo + vbInformation = 68

If Answer = vbYes then
if 68 = 4 then
if False then

68 does not equal 4!!!!!!!!!!!!!!!!!!!!!!!!!!!


"(E-Mail Removed)" wrote:

> Good afternoon all,
>
> I have the below macro. I entered an addition line for when the user
> presses the cancel box, but the macro keeps going after I press
> cancel. I'd like it to stop and go back to the main menu.
>
>
> SORT_VENDOR
>
> Dim sUsername As String
> Dim sPrompt As String
>
> sPrompt = "Please enter vendor name"
> sUsername = InputBox(sPrompt, sTitle, sDefault)
>
> Sheets("list").Select
> Range("C3").Select
> Cells.Find(What:=sUsername, After:=ActiveCell, LookIn:=xlFormulas,
> LookAt _
> :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
> MatchCase:= _
> False, SearchFormat:=False).Select
> If sUsername = "" Then
> MsgBox "You cancelled the search."
> Sheets("welcome").Select
> Range("a1").Select
>
> Exit Sub
>
> Answer = MsgBox("Is this the contract/vendor you would like to
> delete?", vbYesNo + vbInformation, "Please Confirm")
> If Answer = vbYes Then
> Select Case LCase(Range("B" & Selection.Row).Value)
>
> Case "monthly"
> Selection.Resize(12, 1).EntireRow.Delete
> Case "quarterly"
> Selection.Resize(4, 1).EntireRow.Delete
> Case "yearly"
> Selection.EntireRow.Delete
> End Select
> Exit Sub
>
> Else
> Cells.Find(What:=sUsername, After:=ActiveCell, LookIn:=xlFormulas,
> LookAt _
> :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
> MatchCase:= _
> False, SearchFormat:=False).Activate
> End If
>
> DELETE_EMPTYROWS
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cancelling query launched from macro Howard Brody Microsoft Access Macros 2 21st Jun 2007 06:12 PM
Cancelling a macro Naive Microsoft Excel Programming 1 16th Mar 2005 03:29 PM
Cancelling AutoFilter in a macro =?Utf-8?B?Rmxvc3M=?= Microsoft Excel Programming 2 5th Jul 2004 12:57 PM
end Macro after cancelling open dialog Microsoft Excel Programming 1 29th Jun 2004 08:58 AM
Cancelling query launched from macro Larry Microsoft Access Macros 0 29th Aug 2003 09:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:49 AM.