Have an Excel Open Itself and Save uniquely each time using VBA

Michael515

Board Regular
Joined
Jul 10, 2014
Messages
136
Hi,

I'm trying to have an excel open itself up in the background (so I don't have to actively open it) and then once open run a task where it refreshes the workbook (I have created a refresh workbook button labeled "Refresh_Workbook()" and then save itself uniquely each time it does that (so eventually it has multiple file names with a time stamp). This is the code I'm currently using:

Code:
Private Sub Workbook_Open()
    Workbooks.Open Filename:="C:\Users\Person\Desktop\My Documents\WorkbookRefreshProject.xlsm"
    Application.OnTimeValue ("10:08:00"), "Refresh_Workbook()"
    ActiveWorkbook.SaveCopyAs ("[URL="file://\\filepath\Feed"]\\filepath\Feed[/URL] Program\FormFLow to C:\Users\Person\Desktop\My Documents\WorkbookRefreshProject-" & Format(Now(), ("mmddyyyy") & ".xlsm"))
End Sub

Let me know if you have any questions or comments.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I also realized I can use Windows Task Scheduler to open the Excel, so now I just need to code to refresh the workbook and save it uniquely each time (I don't want the workbooks to save over themselves, if I have task scheduler run 8 times in a day, I want to have 8 saved workbooks), and then close itself, here's my new code:

Code:
Sub Auto_Open()
    Workbooks("WorkbookRefreshProject").RefreshAll
    ActiveWorkbook.SaveCopyAs Filename:="C:\Users\T622577\Desktop\My Documents\WorkbookRefreshProject-" & Format(Now(), ("mmddyyyy") & ".xlsm")
End Sub
 
Upvote 0
Okay so now I got the code for it to work with refresh and add a time in, now I just want to date it as well as well before the time:

Code:
Sub Auto_Open()
    Workbooks("WorkbookRefreshProject").RefreshAll
    ActiveWorkbook.SaveCopyAs Filename:="C:\Users\T622577\Desktop\My Documents\WorkbookRefreshProject- " & Format(Now(), "hh-mm-ss") & ".xlsm"
    ThisWorkbook.Saved = True
    Application.Quit
   
End Sub
 
Upvote 0
Just add the formatting (mmddyyyy) before the time:
Code:
Sub Auto_Open()
    Workbooks("WorkbookRefreshProject").RefreshAll
    ActiveWorkbook.SaveCopyAs Filename:="C:\Users\T622577\Desktop\My Documents\WorkbookRefreshProject- " & Format(Now(), "[COLOR=#ff0000]mmddyyyy [/COLOR]hh-mm-ss") & ".xlsm"
    ThisWorkbook.Saved = True
    Application.Quit
   
End Sub
 
Upvote 0
Just add the formatting (mmddyyyy) before the time:
Code:
Sub Auto_Open()
    Workbooks("WorkbookRefreshProject").RefreshAll
    ActiveWorkbook.SaveCopyAs Filename:="C:\Users\T622577\Desktop\My Documents\WorkbookRefreshProject- " & Format(Now(), "[COLOR=#ff0000]mmddyyyy [/COLOR]hh-mm-ss") & ".xlsm"
    ThisWorkbook.Saved = True
    Application.Quit
   
End Sub

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

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