schtasks /create /ru system /sc hourly /st 08:00:00 /tn SaveExcel /tr "cmd /c cscript c:\SaveExcel.vbs"
would schedule the task hourly...
Main Topics
Browse All TopicsHi,
I have an excel spreadsheet which i link to another spreadsheet. When i open the sheet it updates the values. Is there any way i can make the spreadsheet open itself, update the values and save itself and close ?
Ideally i would like this to happen every hour.
Thanks in advance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi Sirbounty,
I have created a vbs script as above, or at least i think i have.
Dim objExcel
Set objExcel = CreateObject("Excel.Applic
strExcel = "C:\Documents and Settings\pl\Desktop\Peter Lai Reports 2007\Stock Related\Excel Exports\Daily Month End Stock Figures Fin.xls"
objExcel.Workbooks.open strExcel
Set objSheet = objExcel.ActiveWorkbook.Wo
objExcel.ActiveWorkbook.Sa
objExcel.Quit
Set objSheet=Nothing
Set objExcel=Nothing
However, when i re-open 'Daily Month End Stock Figures Fin.xls' it asks me if i want to enable automatic refresh. So it does not seem to be updating automatically.
I am quite new to this so perhaps i have made a simple mistake.
Thanks
Peter
Try this...
Dim objExcel
Set objExcel = CreateObject("Excel.Applic
strExcel = "C:\Documents and Settings\pl\Desktop\Peter Lai Reports 2007\Stock Related\Excel Exports\Daily Month End Stock Figures Fin.xls"
Application.DisplayAlerts = False
objExcel.Workbooks.open strExcel
Set objSheet = objExcel.ActiveWorkbook.Wo
Application.DisplayAlerts = True
objExcel.ActiveWorkbook.Sa
objExcel.Quit
Set objSheet=Nothing
Set objExcel=Nothing
Take this file
''''''''''''''''''''''''''
Dim objExcel
Set objExcel = CreateObject("Excel.Applic
strExcel = "C:\Documents and Settings\pl\Desktop\Peter Lai Reports 2007\Stock Related\Excel Exports\Daily Month End Stock Figures Fin.xls"
Application.DisplayAlerts = False
objExcel.Workbooks.open strExcel
Set objSheet = objExcel.ActiveWorkbook.Wo
Application.DisplayAlerts = True
objExcel.ActiveWorkbook.Sa
objExcel.Quit
Set objSheet=Nothing
Set objExcel=Nothing
''''''''''''''''''''''''''
Save it as SaveExcel.vbs
Now, click Start->Run->CMD /k schtasks /create /ru system /sc hourly /tn SaveExcel /tr "cmd /c cscript c:\SaveExcel.vbs"
This should leave a window open determining if the task was successfully created or not. The vbs should not be opening notepad... : \
Does the worksheet have to be activated?
Dim objExcel
Set objExcel = CreateObject("Excel.Applic
strExcel = "C:\test.xls"
objExcel.Application.Displ
objExcel.Workbooks.Open strExcel
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
objExcel.Application.Displ
objExcel.ActiveWorkbook.Sa
objExcel.Quit
I don't suppose you could upload the two files? www.ee-stuff.com
If there's anything sensitive in there, then don't...
That's what I get for piece-milling code...try this...
Dim objExcel
Set objExcel = CreateObject("Excel.Applic
strExcel = "C:\test.xls"
objExcel.Application.Displ
objExcel.Workbooks.Open strExcel
Set objWorksheet = objExcel.objWorkbook.Works
objWorksheet.Activate
objExcel.Application.Displ
objExcel.ActiveWorkbook.Sa
objExcel.Quit
Your file has successfully been uploaded!
To download the file, you must be logged into EE-Stuff. Here are two pages that will display your file, if logged in:
View all files for Question ID: 22117389
http://www.ee-stuff.com/Ex
Direct link to your file
http://www.ee-stuff.com/Ex
The excel file are very simple at the moment.
Daily Month End Stock Figures Fin.xls links e2:e5 to the New Microsoft Excel Worksheet.xls located in the link folder. I have highlighted the linked cells in yellow.
Really appreciate your help.
I have a crystal report with looks at an oracle database and pulls back data. I then automate this Crystal report to export into an excel spreadsheet. This will be the New Microsoft Excel Worksheet.xls.
Unfortunately the formatting of the export is not very good so i was going to design a template excel spreadsheet called 'Daily Month End Stock Figures Fin.xls' and import the data from the 'New Microsoft Excel Worksheet.xls' into it. After this was done i was then going to automatically email the 'Daily Month End Stock Figures Fin.xls' to users.
I have only set the 'Daily Month End Stock Figures Fin.xls' to update when its opened and presumed the VBS script would open it, update it and save it.
Sorry if i have be misleading!
I think it's the macros that's hanging it...either lower the macro security (not recommended) or there's a tool where you can sign your macro (selfcert, I thought it was called)...found one here: http://www.excelsig.org/Ti
Incidentally, for troubleshooting purposes, you can add Visible=True in the code to see it load...and there's a couple prompts to see if you can determine what's hanging it. This one's linked to your profile, so I'd have to clean it up to get it working...I'll keep playing with it - maybe something will come up.
Dim objExcel
Set objExcel = CreateObject("Excel.Applic
strExcel = "C:\test.xls"
objExcel.Visible=True
objExcel.Application.Displ
objExcel.Workbooks.Open strExcel
msgbox "Check it"
Set objWorksheet = objExcel.Worksheets(1)
objWorksheet.Activate
msgbox "Check again?"
objExcel.Application.Displ
objExcel.ActiveWorkbook.Sa
objExcel.Quit
Neal asked me to lend a hand--and the problem proved a little trickier than I had anticipated.
I tried using Application.OnTime to open a workbook at regular intervals and found that its Workbook_BeforeClose macro wouldn't run reliably, but that the Workbook_Open macro would. The next problem is to get the macro to wait long enough for your update to occur. I solved this problem using a second Application.OnTime macro, run in alternating fashion.
All the following code goes in a helper workbook that remains open all the time. It opens a second workbook "AlarmClocker.xls", waits 5 seconds, then saves and closes it. It then waits another 10 seconds, and repeats the process. Event macros (Workbook_Open) in AlarmClocker.xls run as expected.
'Code goes in regular module sheet, with Public declarations before any subs or functions. _
If you have two or more subs running in alternating fashion, using public variables avoids run-time errors. _
Public RunWhen As Double
Public cRunWhat As String
Sub StartTimer()
'You need to pick the next time to run the AlarmClock sub. Use one of the following statements customized to your situation
'RunWhen = Int(Now) + TimeSerial(Hour(Now) + 1, 0, 0) 'Run every hour, on the hour
RunWhen = Now + 10 / 86400 'Run every 10 seconds
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub
Sub AlarmClock()
'This sub wakes up the other workbook
Workbooks.Open "C:\VBA\Sample '07\AlarmClocker.xls" 'Put your path & filename here
RunWhen = Now + 5 / 86400 'Allow 5 seconds for the workbook to update itself
cRunWhat = "DoStuff" 'Schedule next run
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub
Sub DoStuff()
'Your code goes here
With ActiveWorkbook.Worksheets(
.Cells(Rows.Count, 1).End(xlUp).Offset(1, 1) = Now 'These statements prove the workbook is updated
.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = .Cells(Rows.Count, 1).End(xlUp) + 1
End With
RunWhen = Now + 10 / 86400 'Arrange the next wake-up call
'RunWhen = Int(Now) + TimeSerial(Hour(Now) + 1, 0, 0) 'Run every hour, on the hour
cRunWhat = "AlarmClock"
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
'Save the workbook, then close it
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
ActiveWorkbook.Close savechanges:=False
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False
End Sub
Brad
Hi Brad & Sirbounty,
I have created a module in an excel spreadsheet but to be honest i'm not really sure what i am doing.
What code should i be putting in section ''Your code goes here ?
Would it be possible for you to upload the sample files with this code in so i can see where everything should go ?
Hope my request isn't to cheeky!
Thanks again.
Peter
I have managed to get part of it working . I created a helper file as above although i haven't put anything in the section 'Your code goes here. As i'm not to sure what i should put here.
I then created a alarmclocker.xls which i am using as my sheet which should update itself from another spreadsheet.
However, when i start the helper.xls it will open the alarmclocker.xls but the data isn't updated. It will then close and reopen at the time specified in the script. If I click refresh data while the alarmclocker.xls is open it will refresh and save.
What i am doing wrong?
It sounds like you were able to replicate my test workbooks and get the code to work the same way I was seeing. The remaining problem is the fact that your workbook isn't getting the updated information.
The part that was labelled "Your code goes here" is where you need to add a statement that forces your workbook to update. It might be as simple as:
ActiveWorkbook.UpdateLink
If you can record a macro that will update your workbook whenever the macro runs, please post its code in this thread. Or if you can describe a series of manual steps that will update the workbook, please post those.
Brad
First of all i disabled the "enable automatic refresh" dialog box found on another thread.
Navigate to:
HKEY_CURRENT_USER
Software
Microsoft
Office
11.0
Excel
Options
In the left pane, click the Options folder.
On the Edit menu, point to New, and then click DWORD Value.
Type QuerySecurity, and then press ENTER.
Double-click the QuerySecurity icon.
Type a valid setting in the Value data box and then click OK. (See the "Valid Settings for the QuerySecurity Entry" section below for a listing of the valid settings.)
This change takes effect the next time that you start Excel.
Valid Settings for the QuerySecurity Entry
The following table contains the valid settings for the QuerySecurity entry: Value Effect
--------------------------
0 Excel prompts you every time that you open a file containing a query or PivotTable that has been configured to refresh automatically.
NOTE: This is the default behavior in Excel 2000 SR-1.
1 Excel will not prompt you when you open a file containing a query or PivotTable that has been configured to refresh automatically. Excel will not refresh the query or PivotTable automatically.
2 Excel will not prompt you when you open a file containing a query or PivotTable that has been configured to refresh automatically. Excel will refresh the query or PivotTable automatically.
This stopped "enable automatic refresh" dialog box popping up.
I then set the data properties to automtically update every minute ( the minimum setting) and increase the value in your script which keeps the Daily Month End Stock Figures Fin.xls open to 80seconds.
This means your helper.xls sheet opens the Stock Figures Fin.xls for 80 second, and the automatic background will refresh the sheet after 60 seconds. So it is working to a fashion although i will have to be carefull as to when i schedule auto export and email.
I have left in running today and will update you both tommorow.
Another question: Is it possible to reduce the automatic background refresh value to less than 1minute?
Thanks,
Peter
Business Accounts
Answer for Membership
by: sirbountyPosted on 2007-01-10 at 04:06:47ID: 18283121
Save this as "SaveExcel.vbs" - use task scheduler to automate the task...
ation") rksheets(1 )
ve
Dim objExcel
Set objExcel = CreateObject("Excel.Applic
strExcel = "C:\test.xls"
objExcel.Workbooks.open strExcel
Set objSheet = objExcel.ActiveWorkbook.Wo
objExcel.ActiveWorkbook.Sa
objExcel.Quit