I am trying to write a VBA script (MS Excel) which allows me to automatically refresh Pivot Tables when the data source is refreshed. I have used the code below where the Pivot Tables are in the "Sheet1 "worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Worksheets("Sheet1").PivotTables("PivotTable1").RefreshTable
End Sub
This code only works when I manually change data within the data source. However, I want the Pivot Tables to refresh when data in the data source changes due to VLOOKUP functions and not a manual input.
Does anyone perhaps have a solution for this?
Thanks.
Have you tried this command? I have a sharepoint site that downloads to my excel file. I added this line of the code to refresh the data:
Workbook.Connections("externaldata").Refresh
Related
I'm creating a dashboard in Excel that watches a different excel spreadsheet we use for data entry. It's important that I avoid locking the spreadsheet out for editing, I first tried using the workbook connections however it locks the spreadsheet so it can't be edited it is even set up a shared one drive, I have tried changing the timer to 5 minutes value but this makes no difference. I have seen on forums that a person had a similar issue but used VB and wrote coding.
I have lots of experience using excel but none using VB I don't know where to start.
what I need help with is a simple code creating to get my dashboard pivot tables to refresh at whatever time in minutes then it will talk to my other spreadsheet.
the code I have managed to write as a novice is:
Public Sub Refresh()
'refresh
ActiveWorkbook.RefreshAll
alertTime = Now + TimeValue("00:05:30") 'hh:mm:ss
Application.OnTime alertTime, "Refresh"
End Sub
but this locks the main spreadsheet for any editing after imputing the VBA code in the module area
I made sure I saved the excel spreadsheet as a macro and changed the options to allow macros
My goal is that a Pivot Table updates automatically when I make changes to the source data. The "normal" code (see below) discussed here in the forum does not work.
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("Pivot").PivotTables("Test").PivotCache.Refresh
End Sub
My setup is the following:
In a tab called "Overview" I have numbers (%) I like to adjust to test different scenarios.
In tab "Constituents" I have my table (called "Input") which is the basis for my Pivot Table. The numbers in this table change, whenever I make changes to the percentages in tab "Overview" (obviously numbers in table are generated with formulas).
In tab "Pivot" I have my Pivot Table called "Test".
I have tried my luck with the code stated above. If I overwrite a formula in my table, the Pivot updates automatically. This means that the code works for that scenario.
However: If the numbers in the table change due to the formula (meaning a change in tab "Overview"), my Pivot Table does not update.
Does someone know a way to solve this problem? Thanks a lot in advance!
I think you have to add the Worksheet_Change function in both sheets in "Constituents" as well as in "Overview"
I have some very simple code to automatically refresh my pivot tables when the data source is updated:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet2.PivotTables("PivotTable1").PivotCache.Refresh
Sheet3.PivotTables("PivotTable2").PivotCache.Refresh
Sheet4.PivotTables("PivotTable3").PivotCache.Refresh
End Sub
Whenever it refreshes, my fields ungroup. For example one is in months the other is in weeks, etc. but when it refreshes they all ungroup and show each individual date. Any ideas?
One thing you can do is to record a macro that groups the data into the format that you want. Then copy the macro section that groups the selection after each pivot table refresh command. So essentially you refresh and regroup each time.
A link that shows how you can use VBA to group pivot table data is http://www.ozgrid.com/forum/showthread.php?t=55370
I hope this helps
How to automatically Refresh Power Pivot Window when i open excel file , without going the Power Pivot Window option in power pivot tab?
Vinoth,
It is possible to write VBA that triggers a 'refresh all' by placing the code in the Workbook module and using the Open_Event to trigger it as the workbook is opened.
If you are using 2010 then you will need to use this highly innovative technique developed by a guy called Tom Gleeson.
If you are using 2013 then things become more straightforward as the data model is formally exposed to VBA. There are several resources out there including this.
HTH
Jacob
There is a Refresh All button in the PowerPivot tab that you can use for this purpose. This avoids having to open the PowerPivot window just for refresh.
A caveat though: From what I remember, this just updates the tables which are linked from your current Excel workbook - for other types of connection, you have to go to PowerPivot window and refresh the respective tables.
Here is some information I found on a solution for your issue in Excel 2013:
http://www.vandeputte.org/2012/07/powerpivot-nuggets-part-18-automate.html
From Excel (not Power Pivot Manager), open:
Data
Connections
Connections
Then, select your data source and click Properties. You should see a checkbox for refreshing the data when the file is opened.
You can also right click on any Power Pivot chart or tables and click PivotTable Option... and than under data you can select Refresh data when opening the file
Excel 2013 allows for complete data model refresh through VBA. I added a button on my dashboard and assigned the following macro:
Sub Refresh()
ActiveWorkbook.Model.Refresh
End Sub
-Melissa
This is an Excel 2010 specific issue. In Excel 2013 and later the Refresh icon on the Excel ribbon does what you'd expect it to. In Excel 2010 however linked tables, the data model and pivot tables/charts aren't refreshed by the Refresh icon.
We have a solution much better and simpler than the one Tom Gleeson describes https://ukanexcel.wordpress.com/2016/11/30/refreshing-power-pivot-in-excel-2010/
I wrote a macro that imports a CSV file into my spreadsheet, using a QueryTable. My goal is to import the CSV data and be able to save it in the spreadsheet for future reference.
However, the QueryTable updates with the external CSV file, of course, and I would like to prevent this (because I want to forget about the external file). Is there a QueryTable setting I can use to prevent refreshes, or is this the wrong functionality to use altogether?
Well.. if your goal is to have the data imported and not to keep up that data up to date (i.e. refreshing again with the CSV) then at the end of your VBA after .Refresh command for the query table, just delete the query table.
e.g.
Sheet1.QueryTables(1).Refresh (False)
Sheet1.QueryTables(1).Delete
Be sure to pass False to refresh command to ask Excel to do forground query, where your code execution will hold till you get your data in. Then your cell where you are adding the query table will be treated as regular cell.
You can use VBA to just open the CSV as a workbook. Something like this.
Set myCSVbook = Workbooks.Open Filename:="C:\dir\myfile.csv", Format:=2 '2 is csv
Once you've done that, you can copy individual cells, or you could copy the whole sheet into another workbook in one line. I think there's a Copy method on the Worksheet object to do that.