Open a file from specific folder if msgbox result is NO

Sufiyan97

Well-known Member
Joined
Apr 12, 2019
Messages
1,538
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I want a VBA to open an excel file from specific folder if msgbox result is no

VBA Code:
Private Sub Workbook_Open()

Dim a As String
Dim b As String

a = MsgBox("Welcome to XXX", vbOKOnly, "XXXX")

a = MsgBox("Are you X", vbYesNo, "XXXX")

If a = vbNo Then
a = MsgBox("Opening X", vbOKOnly, "XXXX")

ThisWorkbook.Save
ThisWorkbook.Close

'And then I want to open a file from specific folder

Else
ThisWorkbook.Save
End If

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You need to open the other workbook before closing the one with the code.
VBA Code:
Private Sub Workbook_Open()

MsgBox "Welcome to XXX", vbOKOnly, "XXXX"

If MsgBox("Are you X", vbYesNo, "XXXX") = vbNo Then
   MsgBox "Opening X", vbOKOnly, "XXXX"
   Workbooks.Open "C:\MrExcel\Test.xlsm"
   ThisWorkbook.Save
   ThisWorkbook.Close
Else
   ThisWorkbook.Save
End If

End Sub
 
Upvote 0
Solution
You need to open the other workbook before closing the one with the code.
VBA Code:
Private Sub Workbook_Open()

MsgBox "Welcome to XXX", vbOKOnly, "XXXX"

If MsgBox("Are you X", vbYesNo, "XXXX") = vbNo Then
   MsgBox "Opening X", vbOKOnly, "XXXX"
   Workbooks.Open "C:\MrExcel\Test.xlsm"
   ThisWorkbook.Save
   ThisWorkbook.Close
Else
   ThisWorkbook.Save
End If

End Sub

Thank you very much Fluff
Perfect

Yes, Correct I need to open the other workbook before closing the one with the code.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,867
Members
449,053
Latest member
Mesh

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