title: Code to select No button in message box
body: <p>Im trying to figure out how to use vba code to select the no button in a messagebox. I have a workbook that opens and closes another workbook. When the workbook closes a message box pops up, i.e."Select yes or no". User normally manually will have to click no. Is there a way to do this with code?</p>
  
  <p>This is the first workbook that opens the second workbook with the message box in it.</p>
  
  <pre><code>Option Explicit
  
  Private Sub DONEBTN_Click()
  Dim WRKBK2 As Workbook
  Dim Name As String
  
  Name = "Someones Name"
  
  Set WRKBK2 = Workbooks.Open("C:\Second Workbook.xlsm")        
  
  WRKBK2.Sheets(1).Range("H7").Value = Name                 'Name        
  
  WRKBK2.SaveAs Filename:="C:\New File Name For Workbook2.xlsm", _
  FileFormat:=52, CreateBackup:=False, local:=True
  
  Application.DisplayAlerts = False
  WRKBK2.Close
  Application.ScreenUpdating = True
  End Sub
  </code></pre>
  
  <p>This is the code located in the second workbook that is under ThisWorkbook Private Sub Workbook_BeforeClose(Cancel As Boolean)</p>
  
  <pre><code>Option Explicit
  Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Dim MSG1 As String
  
   MSG1 = MsgBox("Select yes or no?", vbYesNo, "Message Box")
  
   If MSG1 = vbYes Then
  
   ' Code does something
  
   Else
  
   ThisWorkbook.Save
  
   ThisWorkbook.Close
  
  
   End If
   End Sub
  </code></pre>