VBA only save a copy before closing if the workbook's file names last characters is certain text

annayujeanlee

New Member
Joined
Oct 6, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to only save a copy of a workbook upon closing if the last 7 characters of the workbook's name is results, else does not save. The specific reason for this is because there are different versions of the same workbook that people tend to save to edit on their own and they still contain the word results such as "X09EFE Halloween contest test results copy for Matt.xlsx". In addition, I'd like for the new saved copy version's name to grab the first 7 characters of the workbook name & "contest results". So for example, if the workbook's name is "X09EFE Halloween contest test results.xlsx", I'd like the name to save as "X09EFE contest results". Fairly new to Macro's so all I currently have is a script to save the file upon closing.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim CurrDate As String
CurrDate = Format(Date, "MMDDYY")
With ActiveWorkbook
.SaveCopyAs "C:\Users\Test" & _
Left(.Name, InStrRev(.Name, ".") - 1) & ".xlsx"
End With

MsgBox "New file version saved (v. " & CurrDate & ")"


End Sub
 

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.
.xlsm files can contain macros. .xlsx files cannot. Your name for the example workbook that you want to save a copy of is "X09EFE Halloween contest test results.xlsx" and so cannot contain the macro you posted. In what (.xlsm) workbook will your macro be located?
 
Upvote 0
So sorry. The .xlsx was a typo. The macro will be located in "X09EFE Halloween contest test results.xlsm"
 
Upvote 0
Perhaps something like this.
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim WB As Workbook
    Dim FileToSave As String
    Dim CurrDate As String
    Dim SaveFolderPath As String
    Dim TempFileName As String

    If UCase(Split(Me.Name, ".")(1)) = "XLSM" Then
        SaveFolderPath = "C:\Users\Test"
        FileToSave = Trim(Split(ThisWorkbook.Name, ".")(0))
        If UCase(Right(FileToSave, 7)) = "RESULTS" Then
            Application.ScreenUpdating = False
            FileToSave = SaveFolderPath & "\" & Trim(Left(FileToSave, 7)) & " contest results.xlsx"
            TempFileName = SaveFolderPath & "\TempWB$.xlsm"

            CurrDate = Format(Date, "MMDDYY")

            ThisWorkbook.SaveCopyAs TempFileName
            DoEvents
            Set WB = Workbooks.Open(TempFileName)

            Application.DisplayAlerts = False
            WB.SaveAs Filename:=FileToSave, FileFormat:=xlOpenXMLWorkbook
            DoEvents
            Application.DisplayAlerts = True

            WB.Close False
            DoEvents
            Kill TempFileName
            Application.ScreenUpdating = True
            MsgBox "New file version saved (v. " & CurrDate & ")"
        End If
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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