Excel 2010 - Change data source of pivot table from external to worksheet within workbook - excel

I have been having problems with a pivot table that uses an external .csv file as its source, particularly with dates in the csv file being treated as text. So, I decided to import the csv file into another worksheet and then force the dates to be recognised as such by reformatting the relevant columns.
The problem I now have is changing the existing pivot table to use the data in the new worksheet instead of an external source. If I click on "Change Data Source" button, the "Select a table or range" option is greyed out. If I continue with external data source and click the Choose Connection button and select the new worksheet I get a pop up that says "The type of connection selected cannot be used to create a PivotTable".
I also tried converting the csv file to xlsx but had a similar problem trying to get the existing pivot table to use it instead of the csv as its source.

It's been a while since you asked, but I had this same problem and just discovered a solution.
Basically, you can use VBA to change pivot tables to a new pivot cache. If the new cache uses the same fields then your layout will be preserved.
Create a new pivot table based on the new data source (it can just be empty).
Click in the new pivot.
In the immediate window type "? selection.pivottable.cacheindex" and press enter.
Note the index number of the pivot cache.
In Excel click in the old pivot which you want to change.
In the immediate window type "selection.pivottable.cacheindex = x" where x is the index number you noted in step 4.
Repeat step 6 for every pivot table which you want to use the new source.
Remove the table you created in step 1.
There may to change all tables from one cache to another in a single step, but it's probably not something you need to do often.

Yes, that is an annoying constraint for pivot tables.
Steps to recreate the and change the datasource from .csv to .xls file:
Created a .csv file with headings Name, Birthdate, Sign. Closed the file.
Imported the .csv into a new pivot table PivotTable1 in a new workbook.
Saved the orignal .csv into a new workbook c:\path\to\datasource.xls, naming the worksheet Data and making sure the upper left cell of the data range was in cell A1.
Then I added the following VBA macro and executed it:
Sub getExcelData()
With ActiveSheet.PivotTables("PivotTable1").PivotCache
.Connection = Array( _
Array("ODBC;DSN=Excel Files;"), _
Array("DBQ=c:\path\to\datasource.xls;"), _
Array("DefaultDir=c:\path\to;DriverId=790;MaxBufferSize=2048;PageTimeout=5;") _
)
.CommandText = _
"SELECT `Data$`.Birthdate, `Data$`.Name, `Data$`.Sign" & vbCrLf & _
"FROM `c:\path\to\datasource.xls`.`Data$` `Data$`"
End With
End Sub
The .CommandText syntax for this driver is horribly picky and not at all smart.
YMMV with this code in 2010 or later and/or with different drivers. Date and numeric data are notoriously ill-handled even in later versions of Excel data drivers. I generally make sure that every cell in a numeric or date field has a valid value, or at the very least every cell in the first row of data, before importing.

This helps but for those who dont know where is immediate window you need to go to developer tab and get in the Visual basic Macro window and press Ctrl+G it will open the immediate window at the bottom. Original answered by mendosi It's been a while since you asked, but I had this same problem and just discovered a solution. Basically, you can use VBA to change pivot tables to a new pivot cache. If the new cache uses the same fields then your layout will be preserved.
Create a new pivot table based on the new data source (it can just be empty).
Click in the new pivot.
In the immediate window type "? selection.pivottable.cacheindex" and press enter.
Note the index number of the pivot cache.
In Excel click in the old pivot which you want to change.
In the immediate window type "selection.pivottable.cacheindex = x" where x is the index number you noted in step 4.
Repeat step 6 for every pivot table which you want to use the new source.
Remove the table you created in step 1.
There may to change all tables from one cache to another in a single step, but it's probably not something you need to do often.

Since I'm a new member, I can't add comment nor add vote.
Anyway as I found this page because I have the same problem,
to me, the answer from Ines is correct and the easiest one.
Excel 2010:
Customize The Ribbon....
Commands not in the Ribbon...
PivotTable and PivotChart Wizard...
Add number-3 as new menu (I choose as new menu in the INSERT tab)
Click the Pivot Table which data source need to be changed from external connection to a range or table in the active worksheet.
Click number-3 menu
Click "back" twice
Choose Microsoft Excel List or Database radio button
click "next" then fill the range or the named range in the range-box
Click FINISH
There I can finally change it from external data to a range
Thank you very much, Ines.
(Don't know why someone down voted you).

Related

how to use vba to call out the weird hidden Pivot table data sources

I found a weird stuation in my Excel file and just want to know how the creater made it and how to automatically solve it.
Someone keep sending me an excel file with only one sheet which contains one Pivot table.
The weird thing is that I cannot find the data source which this Pivot Table should link to.
Even I tried to click "unhide" button, there is no hinden sheet in this file.
However, when I double click the cell in this Pivot, there will be one new sheet appear in this file. And this new sheet is the data source of this Pivot.
Is it possible to create a Macro to call out this hidden sheet without I double click it manually?
Thanks in advance.
The data is likely stored in a PivotCache - this data is not found on a worksheet but is saved as part of the file.
https://trumpexcel.com/pivot-cache-excel/
See previous question here on SO:
Recreate Source Data from PivotTable Cache
Alternatively, if you are comfortable with XML and your workbook is saved in xlsx format, you can unzip the file (add a .zip extension and double-click) and find the pivotcache details stored as XML in pivotCacheDefinition1.xml and pivotCacheRecords1.xml in the xl\pivotCache folder.

Filling formulas automatically as data increase and sort dynamically [duplicate]

This question already has answers here:
Excel VBA: AutoFill Multiple Cells with Formulas
(2 answers)
Closed 4 years ago.
I want to sort data in a worksheet, which gets new rows daily, in a second worksheet.
The problem is, if I use the SMALL()-function and fill the cells automatically till the last row (A102482 or something like that), my file gets very huge and laggy (>20mb).
Of course the person adding a new line could expand the formula in the second worksheet into a new row, but this is not userfriendly at all!
What would be the best solution? I thought about vba-code, which counts the entries in the first worksheet, and then runs a code like this
for (i to numberrows; i++) { SMALL(A + i + 2*3, i) }
filling the first few rows in the second worksheet so the excel-file doesn't get too big...
Thanks in advance!
Edit.:
To be more specific:
What I'm asking excel to do is copy a worksheet and have the rows sorted on the second worksheet. And as the table expends, of course the sorted table has more values. This process should be done automatically, with the user only entering new data in the first worksheet and seeing the results in the second worksheet. Having the second worksheet's cells all already populates witht the SMALL() function is not an option, as this would work in my case, but this is way too slow and the files get too big...
I suggest making the second table, the dependent one, out of a PivotTable.
This will grow and shrink with the first sheet every time you refresh the data, which can be set to automatically refresh when you open the file. Here's how:
Use "Insert > Table" to format the first list. Then use "Summarize with PivotTable" or "Insert > PivotTable" (2 names for the same feature) to put your PivotTable on the next sheet.
Put all the columns you want in the second sheet under "Row Labels" on the PivotTable wizard thingy that appears.
Under PivotTable Tools > Design > Layout > Subtotals, choose "Do not show subtotals."
Under PivotTable Tools > Design > Layout > Grand Totals, choose "Off for Rows and Columns."
Under PivotTable Tools > Design > Layout > Report Layout, choose "Show in Tabular Form."
Note that if you don't have something unique at the top of the list in Row Labels, similar to "ID" in my example ( =ROW() ), the PivotTable will do intelligent things with the data that you may not want.
Here's how to get the thing to refresh automatically when the file opens. Alternately, you right-click on the table and choose "refresh".
If you wanted to refresh the PivotTable every time the data changes, put
Sheets("Sheet2").PivotTables(1).PivotCache.Refresh
(substituting "Sheet2" with the name of the sheet where the table appears; activating the data sheet on the left side; select Worksheet on the first pull-down, and select Change on the second pull-down to access the Sub Worksheet_Change code area)
...here:
Cheers!
I think I've found the answer here:
Excel VBA: AutoFill Multiple Cells with Formulas
Sorry that my question was so misleading, basically the above answer is all I was asking for... Thanks for your time and your answers!
edit: linked to the specific post containing the answer

Changing the data source of pivot table in VBA

My problem goes like this:
I have 2 worksheets in 1 workbook which are the "Source Data" and the "Pivot Sheet". Everytime I generate(I generate data every week and it's in macros), data are added to Source Data sheet but the Pivot Table in the Pivot Sheet remains the same. I think it's quite handy if I will still have to Change Data Source everytime I'll generate the report. I want it to automatically adjust it's data source until the last used cell. How to do it? Columns used as Source are from Column C to W Please help! :(
If you create tables (ctrl + t) for your source data and direct your pivot to read those tables, they should automatically expand as new data is entered. Then it should be as simple as Thisworkbook.Worksheets("Pivot").PivotTables("PivotTable1").PivotCache.Refresh (based on the macro recorder)

How to paste from PowerPivot without Data Model

How can I export a PowerPivot table to a new workbook without the attached data model?
Even if I click paste values, Excel makes the file unreasonably large (13MB).
Can I turn this behaviour off?
When you say pasting a Power Pivot table, do you mean copying the table that exists in the data model, that you access through clicking the "Manage" button in the Power Pivot ribbon menu?
Or do you mean copying the values displayed in an Excel pivot table that happens to be backed by a Power Pivot model?
If the former, you can paste the table directly into a new workbook sheet (assuming it has fewer rows than Excel's limit) with no problems and no attachment made to the source workbook's data model.
If the latter, pasting values will bring over only the values, but you have to utilize the right click menu option to do so. If you just CTRL-C CTRL-V the pivot table, and then use the copy-paste context menu to change to values only, then Excel is first creating a copy of the data model in the new workbook to back the pivot table that you are pasting. Then after this action is completed, you are telling it to display values only, but Excel will not delete the data model it's just created to back the pivot table.
I just ran into this problem, it was very easy to resolve. Initially I was trying to paste as values into the destination workbook directly from the source Pivot table which automatically brings the data model across with it.
Simply make a copy of the table by pasting values into the SAME workbook as your data model. Then copy and past the new table into your destination workbook, it will just give you the values and leave the data model behind.

How can I use a data connection from another book?

Under Data -> Conections I have added a link to another excel worksheet. I'm trying to use some value from this new linked worksheet but I dont know how.
I'm researching this, because using [file] or indirect() is not enough.
How could I reference a cell from a defined data connection from another worksheet in a linked excel file?
I fear that even a highly simplified answer to your question may require pages and pages of detail (for example, not counting the Connections icon set in the Ribbon, for Get External Data I have five icons in the standard Ribbon to choose from and one of these offers me seven choices) so merely an example:
In a new workbook, enter 666666 in Sheet2 B2, select B1:B2 and INSERT > Tables, Table, check My table has headers and OK. Save this workbook as WB1.xlsx, somewhere you can relocate it, and close it.
Open a new workbook and select A2 in Sheet1. DATA > Get External Data, Existing Connections. Browse for Moreā€¦, find and select WB1.xlsx. Open. A Select Table window should then pop up and from it Select Sheet2$. OK. OK. In A3 of this new workbook you should now see 666666.

Resources