Stop some code

Fritz24

Board Regular
Joined
Sep 8, 2004
Messages
102
I'm sure this is really easy but I can't think of how to do it.

I've created a basic bit of code that will go at the start of a macro. It's just to remind the user of the database that they have to do something before they continue.

The code makes a vbYesNo message box appear. What I want it to do is stop the macro if they press No, and continue if they press Yes. The Yes bit is ok but I can't stop the macro.

Please can someone advise?

Thanks.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Can you post what code you have already?

Generally something like the following will work:
Code:
Sub Test()
Dim Continue

     Continue=Msgbox("Do you want to continue?", vbYesNo)
     
     If Continue = vbYes Then
         ' run code
     Else
         MsgBox "Procedure Cancelled"
         Exit Sub
     End If

End Sub

BTW You don't really need the Else, but you might want to display some message to the user.
 
Upvote 0
This is what I tried:

Public Function Update()

Dim strUpdated As String

strUpdated = MsgBox("Please confirm you have done what you needed to.", vbYesNo, "Warning")

If strUpdated = vbNo Then

End Function


If I was to use your method how do I call the subroutine from the macro?
 
Upvote 0
Hi,

You could try using two macros:eek:ne that tests if they want to continue, and one if they choose yes.

The first should have the action RunCode, with a function name "Update"

This will run your Update function, something like:
Code:
Function Update()
Dim intUpdated As Integer
Dim strConfirm As String
Dim mcrMyCode As String

    strConfirm = "Please confirm you have done what you needed to."
    mcrMyCode = "YourMacro"

    intUpdated = MsgBox(strConfirm, vbYesNo, "Warning")
    If intUpdated = vbYes Then
        DoCmd.RunMacro mcrMyCode
    End If

End Function

If they click yes, the code will run the second macro (please change "YourMacro" to your macro name!),if they click no, nothing will happen.

Don't forget to remove the RunCode action from your original macro!
 
Upvote 0

Forum statistics

Threads
1,221,829
Messages
6,162,229
Members
451,756
Latest member
tommyw

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