Macro to close specific Files Except the Active Workbook

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to close all files containing "Sales Incentives" in the name of the workbook eg BR1 Sales Incentives (Pin).xlsm, Southern Sales incentives(Pin).xlsm etc except for the active workbook

However, these files are being closed

Code:
 Sub Close_Incentive_Workbooks()
 
    Dim wb As Workbook
    For Each wb In Application.Workbooks
        If wb.Name Like "*Sales Incentives.xlsm*" Then
           If Not (wb Is ActiveWorkbook) Then wb.Close SaveChanges:=True
        
                   End If
    Next wb
    
End Sub


It would be appreciated if someone could kindly amend my code
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Maybe something like this (not tested)

VBA Code:
Sub Close_Incentive_Workbooks()
    
    Dim wb As Workbook
    For Each wb In Application.Workbooks
        If wb.Name Like "*Sales Incentives.xlsm*" Then
            If wb.Name <> ActiveWorkbook.Name Then
                wb.Close SaveChanges:=True
            End If
        End If
    Next wb
    
End Sub
 
Upvote 0
Hi,
see if this update to your code helps

VBA Code:
Sub Close_Incentive_Workbooks()
    Dim wb As Workbook
    For Each wb In Application.Workbooks
        If Not (wb Is ActiveWorkbook) Then
            If LCase(wb.Name) Like "*sales incentives*" Then wb.Close True
        End If
    Next wb
End Sub

Dave
 
Upvote 0
Thanks for the help

I amended this code


Code:
 If wb.Name Like "*Sales Incentives.xlsm*" Then

to

If wb.Name Like "*Sales Incentives*.*.xlsm*" Then
 
Upvote 0
welcome - glad we were able to help

Dave
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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