1

I am trying to write a vba script to automatically open an excel file increase the day by one in a certain cell and then wait 2 minutes for all other field to populate information from the current day. Once the other field are populated I want the script to close the file and save as the day it ran the script. I am going to add this script to windows task manager to run daily at 2 am. so far I have something like this.

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("c:\Users\dr\Desktop\test.xlsx")

objExcel.Application.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(3, 2).Value = "=today()"

WScript.Sleep 120000

wb.SaveAs Format( Now(), “DD-MMM-YY”)
1
  • Where exactly are you running into problem?
    – PankajR
    Feb 1, 2016 at 14:09

2 Answers 2

3

Your script needs to open the Workbook, then update the day, calculate ("populate the other cells"), save and close. My advice is to not use Sleep for this, but just force the Workbook to calculate and then close it. Like @iDevlop says: Changing the formula in B3 is unneccessary. The same goes for actually showing the Excel application.

The following opens the workbook, recalculates (so the B3 value will be today's date and the rest of the workbook will calculate accordingly), and saves:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("c:\Users\dr\Desktop\test.xlsx")
objExcel.Calculate
objWorkbook.SaveAs Format( Now(), “DD-MMM-YY”)

For info on .calculate see https://msdn.microsoft.com/EN-US/library/office/ff195517.aspx

2

Not very sure to fully understand your question, but I already see one or 2 fixes:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("c:\Users\dr\Desktop\test.xlsx")

objExcel.Application.Visible = True
'objExcel.Workbooks.Add
objWorkbook.Sheets("sheetName").Cells(3, 2).Value = "=today()"

WScript.Sleep 120000

objWorkbook.SaveAs "F" & format(date(), "yyyymmdd") & ".xlsm", 52

Anyway, changing the formula in B3 to "=Today()" everyday is totally useless, since it was already that same formula the day before.


Edit: if you are running this from an Excel VBA procedure, you don't need to create a new instance of Excel:

dim sFolder as string
sFolder = "c:\Users\dr\Desktop\"
Set objWorkbook = Workbooks.Open(sFolder & "test.xlsx")
objWorkbook.Sheets("sheetName").recalc
objWorkbook.SaveAs sFolder & format(date(), "yyyymmdd") & ".xlsx"
objWorkbook.Close
13
  • Thanks for the help. I am getting an error Line 10 Char 56 Error Invalid Character Code 800A0408 Source Microsoft VBScript copilation error. Also for the Save as function how can add something like test.day.xlms
    – Danny R
    Feb 1, 2016 at 15:50
  • For the SaveAs, look at stackoverflow.com/a/25214233/78522. For the rest, look at my Edit
    – iDevlop
    Feb 1, 2016 at 16:41
  • Can you tell me how I can add to the script after it does the sleep I want the script to select the entire worksheet do a copy and past as a special value of "values". I still can't get the saveas function working. I want the file to save as in the path "c:\Users\dr\Desktop\test-today.xlsx")
    – Danny R
    Feb 1, 2016 at 20:27
  • I got most of my code working. No matter what I do I can't get the objWorkbook.SaveAs Method working properly. I want to save the file in a specific location with the name+date or just the date as the name does not matter. It should be a xlsx file format. Please help.
    – Danny R
    Feb 2, 2016 at 15:57
  • @DannyR XLSX is format without macro, the sheet will save without macro. To get that, just drop the , 52 at the end of the SaveAsline, and perhaps add a full path in front of the name. Do you have an error ? which one ?
    – iDevlop
    Feb 2, 2016 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.