VB YESNOCANCEL Action

KayWill

New Member
Joined
Jan 22, 2015
Messages
36
I have a module that is working but when it ends, instead of staying on the spreadsheet, it goes back to the VB code, what am I doing wrong below:

Code:
Sub UserInput()

Dim iReply As Integer
Dim Response As Variant
Dim iNum As Integer

ActiveSheet.Unprotect "XXXXX"

    iReply = MsgBox(Prompt:="Do you wish change the current base uplift of 20%", _
            Buttons:=vbYesNoCancel, Title:="UPDATE UPLIFT %")
       
    If iReply = vbYes Then
        ' Accept Number from the user
        iNum = Application.InputBox("Please Enter Your New Uplift:", "Uplift: Accept Number", , , , , , 1)
        Worksheets("DataCollection").Range("AF3").Value = iNum / 100
      Exit Sub
    ElseIf iReply = vbNo Then
        'Go Back to spreadsheet
        MsgBox ("Continue to add Program data"):
      End If
      
ActiveSheet.Protect "XXXXX", True, True
 
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi there,

I'm not sure I understand. Are you running this routine from the VBE? If so it will put focus back there when done. Perhaps you could explain what it is that should be happening on a Yes, No and Cancel clicks? If you want to exit a routine, the line of code you need is this...

Code:
Exit Sub

Also, instead of declaring iReply as an Integer, I'd recommend declaring it as a VbMsgBoxResult type. This gives you better access to intellisense.
 
Upvote 0
Thank you Zack, I will try your suggestions and follow-up. To respond to your question in the first paragraph, If the person clicks 'Yes', then it allows them to change the base percentage and their answer is added to a specific cell ("af3") then returns them to the spreadsheet (DataCollection), if the answer is 'No' or 'Cancel', it should just return them to the spreadsheet to continue to add more data.
 
Upvote 0
Thank you Zack, I will try your suggestions and follow-up. To respond to your question in the first paragraph, If the person clicks 'Yes', then it allows them to change the base percentage and their answer is added to a specific cell ("af3") then returns them to the spreadsheet (DataCollection), if the answer is 'No' or 'Cancel', it should just return them to the spreadsheet to continue to add more data.

Am I placing the
in the wrong place? I tried to place a secondary 'Exit Sub' after the "if no" section
ElseIf iReply = vbNo Then
'Go Back to spreadsheet
MsgBox ("Continue to add Program data"):
End If
Exit Sub

ActiveSheet.Protect "DARKOicr", True, True

End Sub

but it still takes me back to the VBA code, any assistance will be greatly appreciated.
 
Upvote 0
It all depends on how/when/where you are calling this procedure from. Like I said, if you call it from the VBE, you'll be returned there unless the focus is somewhere else first, like the spreadsheet. Assuming that you're not going to be opening the VBE whenever you want to run this code, this seems like an issue which will work itself out when you've placed the action (to call the routine) where you want it (e.g. button, whatever).

Unless you're doing anything else with your code I would remove all 'Exit Sub' lines. Look at your process: 1) unprotect sheet, 2) prompt user, 3) perform actions based on response, 4) protect sheet. If you exit on step 3, you've already done step 1 (unprotect sheet) and would therefore skip step 4 (reprotect sheet). If anything I would use a GoTo line statement to bring them to step 4, thus skipping any other steps you would want to add after step 3. But if this is all of your code it's pointless, as they'll exit right after the response check and protecting the sheet.

As far as the logical process of your code goes, you've only outlined two conditions on a three condition message box result. You are changing a value if a Yes is clicked, based on an input box value (which could be anything, which you're not checking for, as an example, what if a user enters 0?). You are only giving another message box if No is clicked, but then doing nothing. And you're not even doing anything if Cancel is clicked. As far as I can tell you really only need a Yes/No or Ok/Cancel message box. If this is the case, I would do something like this, which has checks to ensure proper numerical entry...

Code:
Sub UserInput()

    Dim iReply As VbMsgBoxResult
    Dim Response As Variant
    Dim iNum As Variant

    ActiveSheet.Unprotect "XXXXX"

    iReply = MsgBox(Prompt:="Do you wish change the current base uplift of 20%", _
                    Buttons:=vbYesNo, Title:="UPDATE UPLIFT %")

    If iReply = vbYes Then
        ' Accept Number from the user
        On Error Resume Next
        iNum = Application.InputBox("Please Enter Your New Uplift:", "Uplift: Accept Number", , , , , , 1)
        On Error GoTo 0
        If IsNumeric(iNum) Then
            If iNum <> 0 Then
                If Int(iNum) = iNum Then
                    Worksheets("DataCollection").Range("AF3").Value = iNum / 100
                Else
                    If iNum >= 1 And iNum <= -1 Then
                        Worksheets("DataCollection").Range("AF3").Value = iNum
                    Else
                        MsgBox "Please enter a whole number or percent.", vbExclamation, "UPDATE UPLIFT %"
                    End If
                End If
            End If
        End If
    End If

    ActiveSheet.Protect "XXXXX", True, True

End Sub

The above code has some error handling as well as value checking. Users are a fickle thing, and it's best to account for all of their nuances. For example someone might enter a 0.2 rather than 20, but both still may only want 20%. The code above checks if they've first entered a number, then if the number is a whole number or not (no check for negative values at this point, but could be added if it was your requisite). Then there is a check if the number is greater than or equal to -1 and less than or equal to 1, in which case we're assuming they're entering a percentage value and we don't need to divide by one hundred. If a user tries entering a decimal value greater than 1 or less than -1 then they receive the message box "Please enter a whole number or percent."

Again, call this from your worksheet (hook to an event, place on a button/shape click, etc.) and the VBE won't be active when it's done. Call it from the VBE and it will return there when done.

HTH
 
Upvote 0
Thank you so much Zack for your assistance, I review where I have my code, make the changes you so nicely provided and give feedback.
Thanks again for all your assistance.

Kay
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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