Dropdown selection requires YES/NO authorization before script continues

duteberta

Board Regular
Joined
Jun 14, 2009
Messages
92
Office Version
  1. 365
Platform
  1. MacOS
Not sure how to get a message box inside this code.

I would like to have a NO/YES message box if the user selects "SOLD" from the dropdown. A "YES" selection would allow the script to proceed, if "NO" then the "SOLD" selection would not be allowed and the script would stop.

How (where) would I insert the message box into the following code?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim rngCol1 As Range
    Dim rng2 As Range
    Dim LO As ListObject
    Dim lColCnt As Long
    Dim i As Long
    Dim v As Variant

    Set LO = Me.ListObjects(1)
    lColCnt = LO.ListColumns.Count

    Set rngCol1 = Intersect(Target, LO.Range.Columns(3))

    If Not rngCol1 Is Nothing Then
        With Application
            .EnableEvents = False
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With

        ReDim v(1 To lColCnt)

        For Each rng In rngCol1
            If LCase(rng.Value) = LCase("SOLD") Then
                i = 0

                'Remember the numerical formats of each column
                For Each rng2 In LO.ListRows(rng.Row - LO.Range.Row).Range
                    i = i + 1
                    v(i) = rng2.NumberFormat
                Next rng2

                '"paste" values
                rng.Resize(, lColCnt).Value = rng.Resize(, lColCnt).Value

                'Restore original formats of each column
                For i = 1 To lColCnt
                    LO.ListRows(rng.Row - LO.Range.Row).Range.Cells(1).Offset(, i - 1).NumberFormat = v(i)
                Next i
            End If
        Next rng

SortTable

        With Application
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
        End With
    End If

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Here is a simple block of code that shows you how you can do that:
VBA Code:
    Dim resp
    resp = MsgBox("Do you want to continue?", vbYesNo)
    If resp = vbYes Then
        'continue with code here
    Else
        Exit Sub
    End If
 
Upvote 0
Where exactly would I insert the code? After the Dim declarations?
 
Upvote 0
Where exactly would I insert the code? After the Dim declarations?
Wherever/whenever you want it to pop-up! That call is yours.
If you want it to be the first thing that happens, then right after the variable declarations make sense.

Though you may want to check to make sure that you are in the correct range first.
Otherwise, it will fire anytime any change is made in your sheet.
 
Upvote 0
It needs to come up when someone selects "SOLD" from the dropdown.

So maybe after

VBA Code:
        For Each rng In rngCol1
            If LCase(rng.Value) = LCase("SOLD") Then
                i = 0
 
Upvote 0
It needs to come up when someone selects "SOLD" from the dropdown.

So maybe after

VBA Code:
        For Each rng In rngCol1
            If LCase(rng.Value) = LCase("SOLD") Then
                i = 0
Try it and see how it works out for you, and if everything is behaving like you want.

Note that because you have a lot of stuff going on after that, it may be best to change the code slightly, and check for when they click "No", and exit there. Otherwise, it can just proceed to the rest of the code, i.e.
VBA Code:
    Dim resp
    resp = MsgBox("Do you want to continue?", vbYesNo)
    If resp = vbNo Then
        Exit Sub
    End If
    'rest of code
 
Upvote 0
Ya I can't get it to behave right. If someone selects "NO" then it should exit the sub and the dropdown value should not be allowed to change. Currently the cell value stays "SOLD" even when clicking NO
 
Upvote 0
Ya I can't get it to behave right. If someone selects "NO" then it should exit the sub and the dropdown value should not be allowed to change. Currently the cell value stays "SOLD" even when clicking NO
Understand that the code you have runs AFTER the drop-down box update happens, not before it.
So it cannot "stop" the update from happening, because the update already happened by the time this code ran.
You could set the value in that drop-down to some other value if they click "No", if you like. You would just add that line right above the "Exit Sub" line.
 
Upvote 0
Add this new sub to your module:
VBA Code:
Sub Undo_()
    With Application
        .EnableEvents = False
        .Undo
        .EnableEvents = True
    End With
End Sub

An then incorporate the sub in the Joe code as follows :
VBA Code:
    Dim resp As VbMsgBoxResult
    resp = MsgBox("Do you want to continue?", vbYesNo)
    If resp = vbNo Then
        Call Undo_
        Exit Sub
    End If
   
    'rest of code
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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