SaveAs During BeforeClose Event

Woodpile

New Member
Joined
Sep 14, 2007
Messages
38
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello VBA Mystics,

I am having a issue trying to save a file as a different type name and path. The base file is an xlsm. I want to save as an archived copy when the user closes the workbook without any other action.

My real problem is the code works fine on my machine but not on any other, even under my log in. The variables are showing values when other users hover over them, so that part seems to be working.

Here is the full code for the event:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

On Error Resume Next

Dim REPORTWEEK As Long, REPORTDATE As Date

If [WEEKDAY(TODAY())] < 6 Then REPORTWEEK = [ISOWEEKNUM(Today())]

If [WEEKDAY(TODAY())] = 6 Then REPORTWEEK = [ISOWEEKNUM(Today())] + 1

If [WEEKDAY(TODAY())] < 6 Then REPORTDATE = [TODAY()] + 1

If [WEEKDAY(TODAY())] = 6 Then REPORTDATE = [TODAY()] + 3

Application.DisplayAlerts = False

ThisWorkbook.Save

ThisWorkbook.SaveAs "S:\Maintenance Cost Control\Planning Metrics\00- Transition Schedules\Schedule Archives\WK-" & [REPORTWEEK] & " " & [REPORTDATE] & " Schedule.xlsx", FileFormat:=51

Application.DisplayAlerts = True

Shell "explorer.exe " & "S:\Maintenance Cost Control\Planning Metrics\00- Transition Schedules\Schedule Archives\"

End Sub

I recorded a macro to see if Excel produced a different code. I used a different filename for to remove the variables, just in case:

ActiveWorkbook.SaveAs Filename:= "S:\Maintenance Cost Control\Planning Metrics\00- Transition Schedules\S4 Transition Schedule.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

This works on other computers but I need the filename to use the variables so each day's file is unique.

Any insight is greatly appreciated.


Thanks,


Ed
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I am guessing but suspect the Date/Time Regional settings on some machines might be different.
might help, regional settings are in the upper right corner of Date/Time settings.
 
Upvote 0
Solution
RayFrye,

Thank you for the tip. That worked. The machines that would not save the file had slashes in the date format.
 
Upvote 0
Your are welcome, a little more than a tip as it solved your problem. LOL.
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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