Consulting

Results 1 to 6 of 6

Thread: Update pivot tables with task scheduler

  1. #1
    VBAX Regular
    Joined
    Dec 2007
    Posts
    26
    Location

    Question Update pivot tables with task scheduler

    Hi guys,
    I am fairly new to cutting some vb, so some help would be greatly appreciated.

    Ok my problem, I have a reporting tool which which generates pivot tables into excel but these need manually updating. I would like to setup a job in task scheduler to open the spreadsheet update pivot table/s, save,run a macro which will email the spreadsheet to a distribution list then exit.

    i thought about having the email on close/save, but then i figured when i send it and someone else opens it, the same thing will happen when they open close etc.

    I had a go at this, but clearly failed,

    Dim objExcel
    Dim wks As Worksheet
    Dim pvt As PivotTable
    Set objExcel = CreateObject("Excel.Application")
    strExcel = "C:\test.xls"
    objExcel.Visible=True
    objExcel.Application.DisplayAlerts = False
    objExcel.Workbooks.Open strExcel
    For Each wks In Worksheets
    For Each pvt In wks.PivotTables
    pvt.RefreshTable
    Next pvt
    Next wks
    msgbox "test"
    objExcel.Application.DisplayAlerts = True
    objExcel.ActiveWorkbook.Save
    objExcel.Quit

    i just put the msg box in there to see whether the field updated, i also didn't have a clue how to run the macro.

    Any help would be greatly appreciated.
    Thanks
    gareth

  2. #2
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    YOu can refresh all pivot tables/external data in a workbook by

    ThisWorkbook.RefreshAll

    No need to do each one individually in a loop.

    your code is fine to create the excel object and open the file....but instead of your loop, just open the file and have that workbook do a RefreshAll

    [VBA]
    Dim objExcel as Object
    Dim wks As Worksheet
    Dim pvt As PivotTable
    Dim wb as Workbook
    Set objExcel = CreateObject("Excel.Application")
    strExcel = "C:\test.xls"
    objExcel.Visible=True
    objExcel.Application.DisplayAlerts = False
    Set Wb = objExcel.Workbooks.Open strExcel
    With wb
    .RefreshAll
    .Close TRUE
    End with

    objExcel.Quit[/VBA]


    I am not familiar with task scheduler, my point was just for simplicity...use RefreshAll to save code and time.
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  3. #3
    VBAX Regular
    Joined
    Dec 2007
    Posts
    26
    Location
    hmm no go, I also have the task scheduler under control, thanks

    I get an error when i run saying
    line:1
    char:14
    error: expected end of statement
    code: 800A0401
    source: Microsoft VBscript compilation error

    any thoughts
    I tried changing a few things without any success?

  4. #4
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    Sorry, I can't debug remotely. "it doesn't work" just doesn't give me enough.

    Line 1: char 14 seems to be a good place to start....not sure what you changed, or what you didn't, or what the other errors were indicated. The syntax I used was excel VBA, but I presume that you are not using Excel.

    In VB the syntax would indeed be different as parts of the language in VB won't be as native as VBA would be (particularly with object references).

    What is this code actually running in? because this isn't so much an excel question, but perhaps a VS 2005? or VBScript process question from another application?
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  5. #5
    VBAX Regular
    Joined
    Dec 2007
    Posts
    26
    Location
    Understandable mate, i have actual dropped this in to a notepad doc and saved as a vbs.

  6. #6
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    well, in VBScript, there really isn't the same declaration syntax, so where I added that...it shouldn't be there.

    It will be an object by default.

    Rather, line one char 14 would be just after the text "Dim ObjExcel"

    Remove "as Object" and see where it heads.
    Dim objExcel , wb

    Set objExcel = CreateObject("Excel.Application")
    strExcel = "C:\test.xls"
    objExcel.Visible=True
    objExcel.Application.DisplayAlerts = False
    Set Wb = objExcel.Workbooks.Open strExcel
    With wb
    .RefreshAll
    .Close True
    End With

    objExcel.Quit
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •