Msg Box box pops up when it is not supposed to

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello guys
I am facing a problem with the message box. When the first message box appears and if I press no, then it should stop the macro. But in this case, it is showing the message which is to be displayed at the end of the macro. Need your expertise to solve the mistake.

Rich (BB code):
Sub TestMsgBox()

    
    Dim Answer      As VbMsgBoxResult
    
    Answer = MsgBox("CHECK..." & vbNewLine & "1.VOUCHER TYPE & LEDGER NAME IS ENTERED", vbYesNo, "Run Macro")
    If Answer = vbYes Then
        Sheets("SalesData").Select
        If Sheets("SalesData").Range("A2") = "" Then
            MsgBox "Data Not Entered"
            Exit Sub
        End If
        End If
    Sheets("SalesData").Activate
    Range("A2").Select
    MsgBox ("Done.")
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You haven't told it what to do if it is NOT "vbYes".
So it will just pick up and continue on after the last "End If".

Try this:
Rich (BB code):
Sub TestMsgBox()
    
    Dim Answer      As VbMsgBoxResult
    
    Answer = MsgBox("CHECK..." & vbNewLine & "1.VOUCHER TYPE & LEDGER NAME IS ENTERED", vbYesNo, "Run Macro")
    If Answer = vbYes Then
        Sheets("SalesData").Select
        If Sheets("SalesData").Range("A2") = "" Then
            MsgBox "Data Not Entered"
            Exit Sub
        End Id
    Else
        Exit Sub
    End If

    Sheets("SalesData").Activate
    Range("A2").Select
    MsgBox ("Done.")

End Sub
 
Upvote 0
Solution
Rich (BB code):
Sub SaveAsSalesXML()

    Dim x           As Long, y  As Long
    Dim rngData     As Range
    Dim strData     As String
    Dim strTempFile As String
    Dim usr         As String
    Dim Answer      As VbMsgBoxResult
    Answer = MsgBox("CHECK..." & vbNewLine & "1.VOUCHER TYPE & LEDGER NAME IS ENTERED", vbYesNo, "Run Macro")
    If Answer = vbYes Then
        Sheets("SalesData").Select
        If Sheets("SalesData").Range("A2") = "" Then
            MsgBox "Data Not Entered"
            Exit Sub
        End If
        UnHideSheets
        Dim LastRowInSheetImportSales           As Long
        Dim LastColumnLetterSheetImportSales    As String
        LastColumnLetterSheetImportSales = Split(Cells(1, (Sheets("ImportSales").Cells.Find("*", , xlFormulas, _
                , xlByColumns, xlPrevious).Column)).Address, "$")(1)                                    ' Get last column letter used in Sheets("ImportSales")
        LastRowInSheetImportSales = Sheets("ImportSales").Cells.Find("*", , xlFormulas, , xlByRows, _
                xlPrevious).Row                                                                         ' Find last row # used in Sheets("ImportSales")
        Sheets("ImportSales").Range("A3:" & LastColumnLetterSheetImportSales & _
                LastRowInSheetImportSales + 1).ClearContents                                              ' Clear contents of cells in Sheets("ImportSales")
        Dim LastColumnNumberInRow   As Long
        Dim xmlFile                 As Object
        x = Sheets("SalesData").Range("A2:A" & Sheets("SalesData").Range("A" & Rows.Count).End(xlUp).Row).Rows.Count    ' Get count of rows to write to file
        LastColumnNumberInRow = Sheets("ImportSales").Cells(2, Sheets("ImportSales").Columns.Count).End(xlToLeft).Column    ' Get last column number in row
       LastColumnLetterSheetImportSales = Split(Cells(1, (Sheets("ImportSales").Cells.Find("*", , xlFormulas, _
               , xlByColumns, xlPrevious).Column)).Address, "$")(1)                                    ' Get last column letter used in Sheets("ImportSales")
        If x > 1 Then Sheets("ImportSales").Range("A2:" & LastColumnLetterSheetImportSales & x + 1).FillDown            ' Create range needed to copy
        Sheets("ImportSales").Range("A2").Resize(x, LastColumnNumberInRow).Copy
        strData = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("Text")                                   ' Save contents into strData
        strTempFile = "C:\Users\" & Environ("username") & "\Desktop\Sales.xml"
        CreateObject("Scripting.FileSystemObject").CreateTextFile(strTempFile, True).Write strData                      ' Write the data to file
        Application.CutCopyMode = False
    End If
   Call HideSheets
    Sheets("SalesData").Activate
    Range("A2").Select
    MsgBox ("File saved on Desktop.")
End Sub
 
Upvote 0
Do you understand how "IF...THEN" blocks work?
In your IF...THEN block, where you are checking the ANSWER, you just need to add an ELSE statement with an "Exit Sub" line before the "END IF", like I showed in my reply.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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