open excel in new instance from vba

olikershaw

New Member
Joined
May 4, 2023
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hi All

New to posting on this forum but have used it a lot in the past for assistance.

I have a excel database with a userform which works perfectly but when I close the userform it closes all instances of excel that are open. If excel is not open it works perfectly

Here is the code for workbook open and then workbook close

VBA Code:
Private Sub Workbook_Open()

Application.Visible = False
If ThisWorkbook.ReadOnly Then
MsgBox "This program is already in use, please try again in a few minutes.", vbInformation
ThisWorkbook.Close savechanges:=False

Else

Application.Visible = False
Fault_Reporting.Show
End If
End Sub



Private Sub CommandButton12_Click()
answer = MsgBox("Are you sure you want to exit?", vbYesNo + vbQuestion + vbDefaultButton2, "Warning")
If answer = vbNo Then Exit Sub
Unload Me
ThisWorkbook.Save
Application.Quit
End Sub



Can any one help
 
Last edited by a moderator:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
If you don't need to worry about a personal macro workbook, then you could test if workbooks.count > 1 before quitting the application.
 
Upvote 0
Instead of line
Code:
Application.Quit
insert
Code:
  If Application.Workbooks.Count > 1 Then
    ThisWorkbook.Close
  Else
    Application.Quit
  End If
Artik

(I'm late :-( )
 
Upvote 0
If you don't need to worry about a personal macro workbook, then you could test if workbooks.count > 1 before quitting the application.
How would this work? Excuse my ignorance I am quite new to VBA.
Cheers
 
Upvote 0
Instead of line
Code:
Application.Quit
insert
Code:
  If Application.Workbooks.Count > 1 Then
    ThisWorkbook.Close
  Else
    Application.Quit
  End If
Artik

(I'm late :( )
Hi Tried this. It still Closes the other workbook but then when I reopen the original excel with the userform it comes back.

Cheers
 
Upvote 0
I didn't notice that you were hiding the Excel application at the beginning.
Fix the code to:
VBA Code:
  If Application.Workbooks.Count > 1 Then
    Application.Visible = True
    ThisWorkbook.Close
  Else
    Application.Quit
  End If
Artik
 
Upvote 0
Solution
I didn't notice that you were hiding the Excel application at the beginning.
Fix the code to:
VBA Code:
  If Application.Workbooks.Count > 1 Then
    Application.Visible = True
    ThisWorkbook.Close
  Else
    Application.Quit
  End If
Artik
Perfect.

Thankyou. Works perfect
 
Upvote 0
I didn't notice that you were hiding the Excel application at the beginning.
Fix the code to:
VBA Code:
  If Application.Workbooks.Count > 1 Then
    Application.Visible = True
    ThisWorkbook.Close
  Else
    Application.Quit
  End If
Artik
Hi Artik

With this code whilst only being really picky.

is there a way to not make excel maximise the window if theres a workbook already open.

If i am to open my userform then there want excel to be minimized and stay minimized even when i close the userform.

Thanks for your help
 
Upvote 0
So far I have no idea how to do it. My attempts so far have ended with an "Out of memory" error or displaying a window with another workbook open.

Artik
 
Upvote 0
So far I have no idea how to do it. My attempts so far have ended with an "Out of memory" error or displaying a window with another workbook open.

Artik
Ok Thanks

its only me being picky its not a big issue. Also I use application.ontime to close if someone has kept it open open accident but if you have another excel open and then close the userform it closes other excels after the ontime

Cheers
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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