VBA to close workbook with different name at the end

cdalgorta

Board Regular
Joined
Jun 5, 2022
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi,

So closing a specific workbook is simple. Google knows too.
But what's the VBA to close a workbook that changes name at the end everyday?

Example:
Today: A_AgingDetail20220721ghtr
Tomorrow: A_AgingDetail20220722erts
A. Tomorrow: A_AgingDetail20220723weph

So the only identical part will always be "A_AgingDetail*"
The file is in my downloads and it is a csv file in case this information is necessary

Thank you in advance!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
One thing you can do is to loop through all opened workbooks and find the one that contains the A_AgingDetail string in its name . You can use the Like operator or the vba Instr function .
 
Upvote 0
One thing you can do is to loop through all opened workbooks and find the one you want. You can use the Like operator or the vba Instr function .
Yes after trying for like an hour, finally figured it out

For Each wbk In Workbooks
If wbk.Name Like "A_AgingDetail*" Then
wbk.Close SaveChanges:=False
End If
Next wbk

Question. Would you know how would I change the above formula if I wanted to "select" that windows?
Like the regular
Windows("name").Activate
How would I edit the above to activate a specific workbook?
I tried

For Each wbk In Workbooks
If wbk.Name Like "A_AgingDetail*" Then
Windows("wbk").Activate
End If
Next wbk

But it did not work.
 
Upvote 0
You can just use wbk.Activate

I just wrote a single function that is generic and which could be used for both activating and\or closing the workbook depending on the Action parameter.

Place this in a Standard Module:
VBA Code:
Option Explicit

Enum enumAction
    CloseIt
    ActivateIt
End Enum

Function Act_On_Workbook_By_Partial_Name( _
    ByVal PartialName As String, _
    ByVal Action As enumAction, _
    Optional ByVal SaveChanges As Boolean = True _
) As Boolean

    Dim owb As Workbook
   
    For Each owb In Workbooks
        If owb.Name Like PartialName & "*" Then
            If Action = ActivateIt Then
                owb.Activate
            Else
                owb.Close SaveChanges
            End If
            Act_On_Workbook_By_Partial_Name = True
            Exit For
        End If
    Next owb

End Function

Now you could activate the workbook as follows:
VBA Code:
Sub Test1()

    If Act_On_Workbook_By_Partial_Name("A_AginghgDetail20220721ghtr", ActivateIt) = False Then
        MsgBox "Workbook whose partial name is:" & vbNewLine & _
               """A_AginghgDetail20220721ghtr"" not found!", vbCritical
    End If
   
End Sub

And you could close the workbook using the same function as follows : (SaveChanges is set to TRUE by default)
VBA Code:
Sub Test2()

    If Act_On_Workbook_By_Partial_Name("A_AginghgDetail20220721ghtr", CloseIt) = False Then
        MsgBox "Workbook whose partial name is:" & vbNewLine & _
               """A_AginghgDetail20220721ghtr"" not found!", vbCritical
    End If
   
End Sub

The only difference between the above two tests is the second argument ActivateIt vs CloseIt
 
Upvote 0
Solution
You can just use wbk.Activate

I just wrote a single function that is generic and which could be used for both activating and\or closing the workbook depending on the Action parameter.

Place this in a Standard Module:
VBA Code:
Option Explicit

Enum enumAction
    CloseIt
    ActivateIt
End Enum

Function Act_On_Workbook_By_Partial_Name( _
    ByVal PartialName As String, _
    ByVal Action As enumAction, _
    Optional ByVal SaveChanges As Boolean = True _
) As Boolean

    Dim owb As Workbook
  
    For Each owb In Workbooks
        If owb.Name Like PartialName & "*" Then
            If Action = ActivateIt Then
                owb.Activate
            Else
                owb.Close SaveChanges
            End If
            Act_On_Workbook_By_Partial_Name = True
            Exit For
        End If
    Next owb

End Function

Now you could activate the workbook as follows:
VBA Code:
Sub Test1()

    If Act_On_Workbook_By_Partial_Name("A_AginghgDetail20220721ghtr", ActivateIt) = False Then
        MsgBox "Workbook whose partial name is:" & vbNewLine & _
               """A_AginghgDetail20220721ghtr"" not found!", vbCritical
    End If
  
End Sub

And you could close the workbook using the same function as follows : (SaveChanges is set to TRUE by default)
VBA Code:
Sub Test2()

    If Act_On_Workbook_By_Partial_Name("A_AginghgDetail20220721ghtr", CloseIt) = False Then
        MsgBox "Workbook whose partial name is:" & vbNewLine & _
               """A_AginghgDetail20220721ghtr"" not found!", vbCritical
    End If
  
End Sub

The only difference between the above two tests is the second argument ActivateIt vs CloseIt
Thank you!
 
Upvote 0
Thank you!
Thanks.
BTW, If you have used wbk.Activate, it won't bring the workbook to the front if the latter is minimized to the taskbar.

In order to force the workbook to the front, you will need to use the Window object as follows:
VBA Code:
wbk.Activate
If wbk.Windows(1).WindowState = xlMinimized Then
wbk.Windows(1).WindowState = xlNormal
End If
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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