+ Reply to Thread
Results 1 to 15 of 15

Automatic refresh of data

  1. #1
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Automatic refresh of data

    Hey all - am new here so sorry if it's the wrong place.

    I have a few spreadsheets with external SQL connections and I want to have them refresh automatically around 6am/7am so that when I get in for work about 7.30 - 8.00ish they are all up to date.

    Best way to do this so that it can happen daily?

    Thanks!

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatic refresh of data

    I assume you use Get & Transform to connect to SQL?

    Set Query to refresh on file open. And use task scheduler to open the excel file on set time.
    Or run script from PowerShell.

    Have a read of link below.
    https://www.digitalcitizen.life/adva...task-scheduler
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: Automatic refresh of data

    Great thanks will try task scheduler.

  4. #4
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: Automatic refresh of data

    Is there another way if I have lots of connections, rather than having to go through and set each query to refresh on file open?
    So like to open via task scheduler but set it to then refresh all?

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatic refresh of data

    Hmm? If you set all query to refresh on open once and save. There is no need to set it again.

    Alternately, you can use VBA code to refresh all upon workbook open.
    Please Login or Register  to view this content.
    Then again, use task scheduler to open the file.

    Note: You will not be able to refresh query/connection without opening the file.

  6. #6
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: Automatic refresh of data

    Thanks, I copied that into the VBA code box, but when I open it isn't refreshing automatically

  7. #7
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: Automatic refresh of data

    The VBA code runs fine when I run it but not running when I open the workbook

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatic refresh of data

    Where did you place it? It should be pasted into "ThisWorkbook" module in VBE. Not the standard modules.

  9. #9
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: Automatic refresh of data

    Here....
    Screenshot1.jpg

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatic refresh of data

    So you have it in the worksheet module. Put it in ThisWorkbook module.

  11. #11
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: Automatic refresh of data

    Sorry I am a bit new, how do I do that?

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatic refresh of data

    Double click on "ThisWorkbook" in the left pane of Visual Basic Editor (VBE). Then paste in the code.

  13. #13
    Registered User
    Join Date
    01-07-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: Automatic refresh of data

    Thanks! That works now.
    Only problem I do have is that on the scheduler, I have set it to open a few files, but it's only open the 1st. Unless I close the 1st, then it opens the 2rd. Then when I close the 2nd, then it opens the 3rd etc... any ideas? Should I not have multiple files opening on 1 task?

  14. #14
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatic refresh of data

    You could use batch file using command line to open files. Or use VBScript.

    See links for some examples.
    https://stackoverflow.com/questions/...open-this-file
    https://social.technet.microsoft.com...w7itprogeneral
    https://exceltables.com/run-scheduled-macro/

  15. #15
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Automatic refresh of data

    There is an issue with putting code in the Open Workbook event. It will run whenever you open the workbook unless you navigate to it using Excel and then hold down the shift key while opening it. So if you want to look at the report after the data has been run and you do not want to run the data again, you will have to do this.

    Unfortunately the task scheduler can only open a file, but it cannot run a macro in the file.

    To get around this, I use a two-step process. I create a script, the script uses the Open Workbook event to run a macro in the target workbook. Initially when you open this script, you will be asked to fill in the path name to the target workbook, the name of the target workbook, and the macro in the target workbook that you want to run. When you open the script thereafter it will run the macro automatically unless you use the method of holding down the shift key as described above.

    This wiki has more details: http://www.utteraccess.com/wiki/Schedule_a_Macro_to_Run

    I generally have a directory for all my scripts and another parallel directory for the target reports. I have the target reports in sub-folders in the parallel directory because I usually also have the documentation about that report in the folder too. It just keeps things neater.

    Make a copy of the template script and give it a descriptive name - I usually have the script name mimic the task name.

    One of the advantages of the script is that you can copy the contents of cell B11 to notepad. This is the string you need when you set the task scheduler. It tells the task scheduler to run the script file. You need to check the contents of cell B9. This is the full path to the Excel executable on your system. Use a full path name in Cell B2 also.

    Now as for running the data. You can use refresh all as indicated above in the target macro. The target macro is in a regular module. I have a word of caution on this. Go to the Data Ribbon and select Queries and Connections. Click on each connection name and go to properties. By default Allow Background Refresh is enabled. I suggest disabling it. By disabling it, the code waits until the query is complete before moving on. This is important if you have code such as refreshing a pivot table that follows the refresh.

    That should get you started.

    I do have other code that reads a "configuration table" and allows me to run only queries that I have "checked off" in the table. This same code also allows me to rewrite the SQL code on demand. Normally I use it to pass parameters such as dates, but I have also generated a list of models in an IN statement - this IN statement changes weekly depending on what models are "hot." I put the model list in an excel table, use a formula to generate the list, give the list a name, put the name in another excel table that contains the SQL code an all runs automatically. If you are interested in this, I can dig up a sample.
    Last edited by dflak; 01-07-2019 at 06:04 PM.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Import API Data from web to EXCEL (with automatic refresh)
    By kmaloney01 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-03-2017, 04:16 AM
  2. Replies: 2
    Last Post: 09-29-2013, 06:15 AM
  3. Replies: 1
    Last Post: 09-28-2013, 07:11 PM
  4. Automatic data refresh
    By sjgoodwin in forum Excel General
    Replies: 1
    Last Post: 11-07-2006, 10:28 AM
  5. Query Refresh-Enable Automatic Refresh Dialogue Box
    By Terri in forum Excel General
    Replies: 0
    Last Post: 05-06-2005, 04:06 PM
  6. AUTO_OPEN and Automatic Data Refresh
    By DH in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-22-2005, 06:06 PM
  7. Automatic refresh of external data
    By Keith Streich in forum Excel General
    Replies: 4
    Last Post: 02-25-2005, 11:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1