annayujeanlee
New Member
- Joined
- Oct 6, 2022
- Messages
- 3
- Office Version
- 365
- Platform
- 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
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