{"id":3292,"date":"2021-04-30T09:45:41","date_gmt":"2021-04-30T16:45:41","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=3292"},"modified":"2021-04-30T09:45:43","modified_gmt":"2021-04-30T16:45:43","slug":"how-to-refresh-data-connections-in-excel-vba","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=3292","title":{"rendered":"How to Refresh Data Connections in Excel VBA"},"content":{"rendered":"\n<p>Quick reference:<\/p>\n\n\n\n<p>The first one will &#8220;refresh&#8221; only one file. The second will &#8220;refresh&#8221; all open excel files.<\/p>\n\n\n\n<p><em>ThisWorkbook.RefreshAll<br>ActiveWorkbook.RefreshAll&nbsp;<\/em><\/p>\n\n\n\n<p>With the release of integrated stock pricing in Microsoft Excel, it was unfortunate there wasn\u2019t any sort of setting that allowed for automatic updating of the stock data while the file was open. Even worse, there is currently no way to ensure the data refreshes when you initially open your Excel file. This becomes vitally important if you are relying on your stock data to be up-to-date before making any trading decisions. Let\u2019s look at a few ways we can incorporate VBA code to help remedy the lack of this feature.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"manual\"><strong>Refresh Data Connections Manually<\/strong><\/h2>\n\n\n\n<p>You can trigger the refreshing of your stock data by either using keyboard shortcut <strong>Ctrl+Alt+F5<\/strong> or navigating to your Excel Ribbon\u2019s <strong>Data tab<\/strong> and clicking the <strong>Refresh All button<\/strong> within the <strong>Queries &amp; Connections button group<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"yui_3_17_2_1_1619717518823_98\"><img decoding=\"async\" src=\"https:\/\/images.squarespace-cdn.com\/content\/v1\/52b5f43ee4b02301e647b446\/1609559656572-W51PXG8NNRZI17MJ6C9O\/ke17ZwdGBToddI8pDm48kKnW2jUf34K1iq946rFyL9pZw-zPPgdn4jUwVcJE1ZvWQUxwkmyExglNqGp0IvTJZUJFbgE-7XRK3dMEBRBhUpx7H_-xjwk66IWNUm6eSaGXiNroAjXEANJ9L6IFbjuG4qx4Wju12Yo2D1TqcACo1XI\/Refresh+All+Button.png?format=750w\" alt=\"Refresh All Button.png\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"open\"><strong>Refresh Data Connections When File Opens (VBA)<\/strong><\/h2>\n\n\n\n<p>You can trigger a data refresh when your Excel file is first opened by pasting VBA code into the <strong>Workbook_Open<\/strong> event. Simply double-click the <strong>ThisWorkbook object <\/strong>in the <strong>VBA Project Pane<\/strong> to open the text editor (blank white sheet) within the <strong>Visual Basic Editor<\/strong> (keyboard shortcut <strong>Alt +F11<\/strong>).<\/p>\n\n\n\n<p>Next, paste the below code into the text editor and save.<\/p>\n\n\n\n<p id=\"yui_3_17_2_1_1619717518823_562\">Private&nbsp;Sub&nbsp;Workbook_Open()<br>&#8216;PURPOSE: Run Data tab&#8217;s Refresh All function when file is opened<br><br>ThisWorkbook.RefreshAll<br><br>MsgBox &#8220;Stock Data has been refreshed!&#8221;<br><br>End&nbsp;Sub<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"yui_3_17_2_1_1619717518823_115\"><img decoding=\"async\" src=\"https:\/\/images.squarespace-cdn.com\/content\/v1\/52b5f43ee4b02301e647b446\/1609559672323-4HXQ4GNJOQ5SGLYXTGCE\/ke17ZwdGBToddI8pDm48kM8-pqY656BtfuyqeD_7gvtZw-zPPgdn4jUwVcJE1ZvWhcwhEtWJXoshNdA9f1qD7T-j82ScS_xjTqFYGqFrT72iMt2Zx3C5zpDyHqH-h_uTr3I5Yn_PoKVTkBOPQXgEwQ\/Workbook+Event.png?format=300w\" alt=\"Workbook Event.png\"\/><\/figure>\n\n\n\n<p>The next time you open your Excel file, you should see the message box immediately appear indicating that your stock data has been refreshed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"activate\"><strong>Refresh Data Connections When Sheet Is Activated (VBA)<\/strong><\/h2>\n\n\n\n<p>You can trigger a data refresh when you navigate to a particular spreadsheet tab by pasting VBA code into the <strong>Worksheet_Activate<\/strong> event. Simply double-click the desired <strong>Sheet object<\/strong> in the <strong>VBA Project Pane<\/strong> to open the text editor (blank white sheet) within the <strong>Visual Basic Editor<\/strong> (keyboard shortcut <strong>Alt +F11<\/strong>). The tab name of your sheets will be inside the parathesis next to the object name, so you can easily decipher which sheet object you want to store the code in.<\/p>\n\n\n\n<p>Next, paste the below code into the text editor and save.<\/p>\n\n\n\n<p>Private&nbsp;Sub&nbsp;Worksheet_Activate()<br>&#8216;PURPOSE: Run Data tab&#8217;s Refresh All function when sheet is activated<br><br>ThisWorkbook.RefreshAll<br><br>MsgBox &#8220;Stock Data has been refreshed!&#8221;<br><br>End&nbsp;Sub<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"yui_3_17_2_1_1619717518823_134\"><img decoding=\"async\" src=\"https:\/\/images.squarespace-cdn.com\/content\/v1\/52b5f43ee4b02301e647b446\/1609559690161-YUSXU03YOO5YM4SDWCT7\/ke17ZwdGBToddI8pDm48kBs0ocHOzau24t9H4uusMX1Zw-zPPgdn4jUwVcJE1ZvWhcwhEtWJXoshNdA9f1qD7WT60LcluGrsDtzPCYop9hMAMnD_t6Osls2WhQJMPj7yGu8fDB-vRfNuRqjC_EFtaQ\/Worksheet+Event.png?format=300w\" alt=\"Worksheet Event.png\"\/><\/figure>\n\n\n\n<p>The next time you navigate to the particular tab you stored the code in, you should see the message box immediately appear indicating that your stock data has been refreshed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"interval\"><strong>Refresh Data Connections Every X Seconds (VBA)<\/strong><\/h2>\n\n\n\n<p>Alright, this is for all the day traders out there that want their data automatically refreshed throughout the day. We can utilize VBA\u2019s OnTime functionality to schedule a macro to run and effectively create a loop so that it keeps refreshing at your desired interval.<\/p>\n\n\n\n<p>There are 4 macros in the below code, however, you will only need to call two of them:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>StartRefreshLoop &#8211; Starts the refresh intervals<\/li><li>EndRefreshLoop &#8211; Ends the refresh intervals<\/li><\/ol>\n\n\n\n<p>You can set the public variable \u201cSeconds\u201d to any interval you wish. Since stock data typically refreshes every 15 minutes, you\u2019ll most likely want to set it to 960 seconds.<\/p>\n\n\n\n<p><em>Public&nbsp;RefreshTime&nbsp;As&nbsp;Double<br>Public&nbsp;Const&nbsp;Seconds = 30&nbsp;&#8216;Input Refresh Interval in seconds<br><br>Sub&nbsp;StartRefreshLoop()<br><br>&#8216;User Message indicating loop is beginning<br>&nbsp;&nbsp;MsgBox &#8220;Refreshes will begin to occur at &#8221; &amp; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;the designated interval of &#8221; &amp; Seconds &amp; &#8221; seconds&#8221;<br><br>&#8216;Call the first Refresh<br>&nbsp;&nbsp;Call&nbsp;StartRefreshes<br><br>End&nbsp;Sub<br><br>Sub&nbsp;StartRefreshes()<br><br>&#8216;Calculate Next Refresh Time<br>&nbsp;&nbsp;RefreshTime = Now + TimeSerial(0, 0, Seconds)<br><br>&#8216;Trigger a Refresh with OnTime function<br>&nbsp;&nbsp;Application.OnTime _<br>&nbsp;&nbsp;&nbsp;&nbsp;EarliestTime:=RefreshTime, _<br>&nbsp;&nbsp;&nbsp;&nbsp;Procedure:=&#8221;RefreshConnections&#8221;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;Schedule:=True<br>&nbsp;&nbsp;<br>End&nbsp;Sub<br><br><br>Sub&nbsp;RefreshConnections()<br><br>&#8216;Refresh Data Connections<br>&nbsp;&nbsp;ThisWorkbook.RefreshAll<br><br>&#8216;Start Timer Over Again<br>&nbsp;&nbsp;Call StartRefreshes<br><br>End&nbsp;Sub<\/em><\/p>\n\n\n\n<p><br><em>Sub&nbsp;EndRefreshLoop()<br><br>&#8216;On Error Resume Next<br>&nbsp;&nbsp;Application.OnTime _<br>&nbsp;&nbsp;&nbsp;&nbsp;EarliestTime:=RefreshTime, _<br>&nbsp;&nbsp;&nbsp;&nbsp;Procedure:=&#8221;RefreshConnections&#8221;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;Schedule:=False<br><br>&#8216;User Message indicating loop has ended<br>&nbsp;&nbsp;MsgBox &#8220;Refreshes are no longer occurring&#8221;<br><br>End&nbsp;Sub<\/em><\/p>\n\n\n\n<p>Ref: https:\/\/www.thespreadsheetguru.com\/blog\/vba-refresh-data-connections-excel<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick reference: The first one will &#8220;refresh&#8221; only one file. The second will &#8220;refresh&#8221; all open excel files. ThisWorkbook.RefreshAllActiveWorkbook.RefreshAll&nbsp; With the release of integrated stock pricing in Microsoft Excel, it was unfortunate there wasn\u2019t any sort of setting that allowed for automatic updating of the stock data while the file was open. Even worse, there <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=3292\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,18],"tags":[705,706],"class_list":["post-3292","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-microsoft-office","tag-refresh-data-connections-in-excel-vba","tag-refresh-data-connections-vba"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3292"}],"version-history":[{"count":2,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3292\/revisions"}],"predecessor-version":[{"id":3296,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/3292\/revisions\/3296"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}