How to close the one workbook if I have multiple workbooks open?

dmadhup

Board Regular
Joined
Feb 21, 2018
Messages
146
Office Version
  1. 365
Hi Team,
Happy Friday!

I am using following VBA code to close only the workbook I am working on, but this code closes all workbooks which have been opened. Any idea, please.

Code:
Dim WbCount As Integer, ii As Integer
WbCount = Workbooks.count

If WbCount > 1 Then
        For ii = WbCount To 1 Step -1
            If Workbooks(ii).Name = ThisWorkbook.Name Then
                Application.DisplayAlerts = False
                Application.EnableEvents = False
                Windows(ThisWorkbook.Name).Close SaveChanges:=False
                Application.EnableEvents = True
                Application.DisplayAlerts = True
            End If
        Next ii
    Else
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        Application.Quit
        Application.EnableEvents = True
        Application.DisplayAlerts = True
    End If
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
No Luck!

I am using office 365 to excel. Is not Windows(ThisWorkbook.Name).Close SaveChanges:=False and ThisWorkbook.Close False same thing?
 
Upvote 0
How about
Code:
   Dim WbCount As Long
   WbCount = Workbooks.Count

   If WbCount > 1 Then
      Application.DisplayAlerts = False
      Application.EnableEvents = False
      ThisWorkbook.Close False
      Application.EnableEvents = True
      Application.DisplayAlerts = True
   Else
      Application.DisplayAlerts = False
      Application.EnableEvents = False
      Application.Quit
      Application.EnableEvents = True
      Application.DisplayAlerts = True
   End If
 
Upvote 0
I think the problem of not working any code is that Excel 2016 opens all workbooks in the same instance. So, it forces to close all....

Don't know how to fix this issue...or it's excel bugs...
 
Upvote 0
If you have more than one workbook open in the same instance then the code I supplied, will only close the workbook containing the macro.
That said, as the code will stop when the workbook closes, EnableEvents & DisplayAlerts will both be switched off.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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