+ Reply to Thread
Results 1 to 4 of 4

Yes/No MsgBox looping if No selected

Hybrid View

  1. #1
    Registered User
    Join Date
    11-06-2018
    Location
    Texas
    MS-Off Ver
    Office 365 V16
    Posts
    2

    Yes/No MsgBox looping if No selected

    First, I hope I am posting this correctly as this is my first post.


    I'm trying to set this condition for various ranges of my worksheet to bring up a warning when anyone has entered data into a section they should not be altering. The warning should give them the option accept their changes or cancel which undo's the entry prompting the warning. The VBA below works great if you hit Yes, but it you hit no it undo's the entry but continues to loop the warning until you hit Yes. Any advice?

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
    Dim Ans As Variant
    Dim Msg As String
         
    Set rng1 = Range("A1:J354")
    Set rng2 = Range("C355:C473")
    Set rng3 = Range("H355:I473")
    Set rng4 = Range("K1:GM473")
    
    
    If Not Intersect(Target, Union(rng1, rng2, rng3, rng4)) Is Nothing Then
            Msg = "This cell should not be changed unless specifically intended.  Changes may result in errors throughout this workbook.  Are you sure you want to make changes?"
            Ans = MsgBox(Msg, vbYesNo)
            If Ans <> vbYes Then
                Application.Undo
            End If
        End If
    End Sub

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,512

    Re: Yes/No MsgBox looping if No selected

    You could try this:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
    Dim Ans As Variant
    Dim Msg As String
         
    Set rng1 = Range("A1:J354")
    Set rng2 = Range("C355:C473")
    Set rng3 = Range("H355:I473")
    Set rng4 = Range("K1:GM473")
    
    
    If Not Intersect(Target, Union(rng1, rng2, rng3, rng4)) Is Nothing Then
            Msg = "This cell should not be changed unless specifically intended.  Changes may result in errors throughout this workbook.  Are you sure you want to make changes?"
            Ans = MsgBox(Msg, vbYesNo)
            If Ans <> vbYes Then
                With Application
                    .EnableEvents = False
                    .Undo
                    .EnableEvents = True
                End With
            End If
        End If
    End Sub
    BSB

  3. #3
    Registered User
    Join Date
    11-06-2018
    Location
    Texas
    MS-Off Ver
    Office 365 V16
    Posts
    2

    Re: Yes/No MsgBox looping if No selected

    Perfect, that nailed it! Thanks for the quick help!

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,512

    Re: Yes/No MsgBox looping if No selected

    No problem at all. Happy to help.

    BSB

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Bring up MsgBox when any value selected in Combobox
    By ShakJames in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-15-2016, 12:07 PM
  2. MsgBox only after all selected Macros are run?
    By Nola111 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-21-2014, 10:21 AM
  3. MsgBox looping
    By rajkmr89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2013, 01:46 PM
  4. [SOLVED] MsgBox to Pop Up - When there is no data in a selected Range
    By Sinalk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2013, 08:45 AM
  5. [SOLVED] If Row 9 is not selected, then msgbox
    By VBA FTW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-11-2012, 01:36 PM
  6. Msgbox when worksheet is selected
    By obrien2010 in forum Excel General
    Replies: 2
    Last Post: 08-24-2010, 11:53 AM
  7. Looping Data = TRUE to a MsgBox
    By JMay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2005, 05:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1