+ Reply to Thread
Results 1 to 12 of 12

Message box code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Message box code

    Hello,
    I have a code already written to execute an operation in a selected sheet. (sheet 23)
    I need help writing a code in a message box.
    If I enter in column L (in sheet 23) the word "BAB", the code should show me the message box saying: "Are you sure about the information?" with 2 options below "YES" and "NO".
    If I select yes, I want it to execute my code.
    If I select no, I want it to only make the message box diasppear, without executing anything.
    Thanks for your help.

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Message box code

    Hi.

    Try this code:

    Sub msgboxcode()
        Dim reply As String
        
        If Range("L2").Value = "BAB" Then
            reply = MsgBox("Are you sure about the information?", vbYesNo)
        End If
        
        If reply = vbYes Then
            'EXECUTE YOUR CODE
        End If
    End Sub
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Message box code

    Hi,

    copy this to the Worksheet module of Sheet 23.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim MyQ1 As String
    If Not Target.Column = 12 Then Exit Sub
    If Not Target.Value = "BAB" Then Exit Sub
    MyQ1 = MsgBox("Are you sure about the information?", vbYesNo)
    If MyQ1 = vbNo Then Exit Sub
    
    'YOU CODE HERE
    MsgBox "You clicked yes"
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Message box code

    Sorry if I didn't make myself clear enough.
    The code I already have is activated when selecting "BAB" in any row of the column L

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim myDestSheet As Worksheet
        Dim myDestSheet2 As Worksheet, destRow As Long, destRow2 As Long
        With ThisWorkbook
            If Target.Cells.Count = 1 And Target.Column = 12 And Target = "BAB" Then
                On Error Resume Next
                Set myDestSheet = Worksheets(Target.Offset(, -11).Value)
    
                If Err.Number = 9 Then
                On Error Resume Next
                Set myDestSheet2 = Worksheets(Target.Offset(, -10).Value)
    
                    If Err.Number = 9 Then
                    On Error Resume Next
                    MsgBox "Sheet " & Target.Offset(, -11).Value & Chr(10) & " Does Not Exist " & Chr(10) & " NO DATA WAS ADDED "
                    Err.Clear
                        MsgBox "Sheet " & Target.Offset(, -10).Value & Chr(10) & " Does Not Exist " & Chr(10) & " NO DATA WAS ADDED "
                        Err.Clear
    
                    
                    Else
                        destRow2 = myDestSheet2.Cells(Rows.Count, "a").End(xlUp).Row + 1
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet2.Name).Cells(destRow2, 5)
                        With myDestSheet2
                            .Cells(destRow2, 1) = Target.Offset(, -9)
                            .Cells(destRow2, 2) = Target.Offset(, -8)
                            .Cells(destRow2, 3) = "SIC"
                            .Cells(destRow2, 4) = Target.Offset(, -11)
                        End With
                        Application.EnableEvents = True
                        MsgBox "Sheet " & Target.Offset(, -11).Value & Chr(10) & " Does Not Exist"
                    Err.Clear
    MsgBox " Data was added to Sheet " & Target.Offset(, -10).Value
    Err.Clear
                    End If
                        Else
                    destRow = myDestSheet.Cells(Rows.Count, "a").End(xlUp).Row + 1
                    Set myDestSheet2 = Worksheets(Target.Offset(, -10).Value)
    
                    If Err.Number = 9 Then
                    On Error Resume Next
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet.Name).Cells(destRow, 5)
                        With myDestSheet
                            .Cells(destRow, 1) = Target.Offset(, -9)
                            .Cells(destRow, 2) = Target.Offset(, -8)
                            .Cells(destRow, 3) = "PIC"
                            .Cells(destRow, 4) = Target.Offset(, -10)
                        End With
                        Application.EnableEvents = True
                        MsgBox "Sheet " & Target.Offset(, -10).Value & Chr(10) & " Does Not Exist"
                        Err.Clear
    MsgBox " Data was added to Sheet " & Target.Offset(, -11).Value
    Err.Clear
                        Else
                        destRow2 = myDestSheet2.Cells(Rows.Count, "a").End(xlUp).Row + 1
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet.Name).Cells(destRow, 5)
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet2.Name).Cells(destRow2, 5)
                        With myDestSheet
                            .Cells(destRow, 1) = Target.Offset(, -9)
                            .Cells(destRow, 2) = Target.Offset(, -8)
                            .Cells(destRow, 3) = "PIC"
                            .Cells(destRow, 4) = Target.Offset(, -10)
                        End With
                        With myDestSheet2
                            .Cells(destRow2, 1) = Target.Offset(, -9)
                            .Cells(destRow2, 2) = Target.Offset(, -8)
                            .Cells(destRow2, 3) = "SIC"
                            .Cells(destRow2, 4) = Target.Offset(, -11)
                        End With
                        Application.EnableEvents = True
                    End If
                    End If
            End If
        End With
    End Sub
    What I need to do is to create a message box that appears when I select in column L the word "BAB" to let me confirm that the information entered are true. If I select Yes, I want it to execute the previous code, and if I select No, I want it to dismiss the message without doing anything so I can edit my information again.
    Thanks

  5. #5
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Message box code

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim myDestSheet As Worksheet
        Dim myDestSheet2 As Worksheet, destRow As Long, destRow2 As Long
        With ThisWorkbook
            If Target.Cells.Count = 1 And Target.Column = 12 And Target = "BAB" Then
                '''' NEW PART ''''
                Dim MyQ1 As String
                MyQ1 = MsgBox("Are you sure about the information?", vbYesNo)
                If MyQ1 = vbNo Then Exit Sub
                '''' END OF NEW PART ''''
                On Error Resume Next
                Set myDestSheet = Worksheets(Target.Offset(, -11).Value)
    
                If Err.Number = 9 Then
                On Error Resume Next
                Set myDestSheet2 = Worksheets(Target.Offset(, -10).Value)
            'etc etc etc rest of the code

  6. #6
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Message box code

    Check this:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim myDestSheet As Worksheet, reply As String
        Dim myDestSheet2 As Worksheet, destRow As Long, destRow2 As Long
        With ThisWorkbook
            If Target.Cells.Count = 1 And Target.Column = 12 And Target = "BAB" Then
                On Error Resume Next
                Set myDestSheet = Worksheets(Target.Offset(, -11).Value)
                
                If Target.Value = "BAB" Then
                    reply = MsgBox("Are you sure about the information?", vbYesNo)
                End If
                
                If reply = vbYes Then
                
                If Err.Number = 9 Then
                On Error Resume Next
                Set myDestSheet2 = Worksheets(Target.Offset(, -10).Value)
    
                    If Err.Number = 9 Then
                    On Error Resume Next
                    MsgBox "Sheet " & Target.Offset(, -11).Value & Chr(10) & " Does Not Exist " & Chr(10) & " NO DATA WAS ADDED "
                    Err.Clear
                        MsgBox "Sheet " & Target.Offset(, -10).Value & Chr(10) & " Does Not Exist " & Chr(10) & " NO DATA WAS ADDED "
                        Err.Clear
    
                    
                    Else
                        destRow2 = myDestSheet2.Cells(Rows.Count, "a").End(xlUp).Row + 1
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet2.Name).Cells(destRow2, 5)
                        With myDestSheet2
                            .Cells(destRow2, 1) = Target.Offset(, -9)
                            .Cells(destRow2, 2) = Target.Offset(, -8)
                            .Cells(destRow2, 3) = "SIC"
                            .Cells(destRow2, 4) = Target.Offset(, -11)
                        End With
                        Application.EnableEvents = True
                        MsgBox "Sheet " & Target.Offset(, -11).Value & Chr(10) & " Does Not Exist"
                    Err.Clear
    MsgBox " Data was added to Sheet " & Target.Offset(, -10).Value
    Err.Clear
                    End If
                        Else
                    destRow = myDestSheet.Cells(Rows.Count, "a").End(xlUp).Row + 1
                    Set myDestSheet2 = Worksheets(Target.Offset(, -10).Value)
    
                    If Err.Number = 9 Then
                    On Error Resume Next
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet.Name).Cells(destRow, 5)
                        With myDestSheet
                            .Cells(destRow, 1) = Target.Offset(, -9)
                            .Cells(destRow, 2) = Target.Offset(, -8)
                            .Cells(destRow, 3) = "PIC"
                            .Cells(destRow, 4) = Target.Offset(, -10)
                        End With
                        Application.EnableEvents = True
                        MsgBox "Sheet " & Target.Offset(, -10).Value & Chr(10) & " Does Not Exist"
                        Err.Clear
    MsgBox " Data was added to Sheet " & Target.Offset(, -11).Value
    Err.Clear
                        Else
                        destRow2 = myDestSheet2.Cells(Rows.Count, "a").End(xlUp).Row + 1
                        Application.EnableEvents = False
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet.Name).Cells(destRow, 5)
                        Target.Parent.Cells(Target.Row, 5).Resize(, 7).Copy .Sheets(myDestSheet2.Name).Cells(destRow2, 5)
                        With myDestSheet
                            .Cells(destRow, 1) = Target.Offset(, -9)
                            .Cells(destRow, 2) = Target.Offset(, -8)
                            .Cells(destRow, 3) = "PIC"
                            .Cells(destRow, 4) = Target.Offset(, -10)
                        End With
                        With myDestSheet2
                            .Cells(destRow2, 1) = Target.Offset(, -9)
                            .Cells(destRow2, 2) = Target.Offset(, -8)
                            .Cells(destRow2, 3) = "SIC"
                            .Cells(destRow2, 4) = Target.Offset(, -11)
                        End With
                        Application.EnableEvents = True
                    End If
                    End If
                    
                     Else
                           Target.Value = ""
                    
                     End If
            End If
        End With
    End Sub
    Last edited by jraj1106; 01-16-2013 at 05:41 AM.

  7. #7
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Message box code

    Thanks alot, it works perfectly

  8. #8
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Message box code

    When I select NO, the code execution stops, but the word in column L stays (BAB). How can I clear it?

  9. #9
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Message box code

                '''' NEW PART ''''
                Dim MyQ1 As String
                MyQ1 = MsgBox("Are you sure about the information?", vbYesNo)
                If MyQ1 = vbNo Then Target.Value = "": Exit Sub
                '''' END OF NEW PART ''''

  10. #10
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Message box code

    I have included the code for clearing col L. Check the above code that I had posted previously.Its just this:

    Else
    Target.Value = ""

  11. #11
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Message box code

    Thank you for your help

  12. #12
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Message box code

    You are welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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