lora.johnnson
New Member
- Joined
- Nov 3, 2010
- Messages
- 3
I have several template files that I plan to add the following code to:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim fname As String
Dim MyDate
Dim MyMonth
MyDate = Date
MyMonth = Month(MyDate)
Sheet1.Protect "123"
Sheet2.Protect "123"
Sheet3.Protect "123"
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:="C:\Line 1 Data\" & MonthName(MyMonth) & Day(Date) & Year(Date) & ThisWorkbook.Name
Application.DisplayAlerts = True
End Sub
Problem is that anytime the Data file is opened and then closed it creates another file that is named "November 3 2010 November 3 2010 Data Name" because it is rerunning the above code. Can the above code be modified to add an If Then Else function that says If the File is already in folder "C;\Line 1 Data\" Then ThisWorkbook.Save Else ThisWorkbook.SaveAs Filename:="C:\Line 1 Data\" & MonthName(MyMonth) & Day(Date) & Year(Date) & ThisWorkbook.Name EndIf. I've tried saying just that and it doesn't do anything. I am a beginner so copying code is about the best I can do.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim fname As String
Dim MyDate
Dim MyMonth
MyDate = Date
MyMonth = Month(MyDate)
Sheet1.Protect "123"
Sheet2.Protect "123"
Sheet3.Protect "123"
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:="C:\Line 1 Data\" & MonthName(MyMonth) & Day(Date) & Year(Date) & ThisWorkbook.Name
Application.DisplayAlerts = True
End Sub
Problem is that anytime the Data file is opened and then closed it creates another file that is named "November 3 2010 November 3 2010 Data Name" because it is rerunning the above code. Can the above code be modified to add an If Then Else function that says If the File is already in folder "C;\Line 1 Data\" Then ThisWorkbook.Save Else ThisWorkbook.SaveAs Filename:="C:\Line 1 Data\" & MonthName(MyMonth) & Day(Date) & Year(Date) & ThisWorkbook.Name EndIf. I've tried saying just that and it doesn't do anything. I am a beginner so copying code is about the best I can do.