kprincehouse
New Member
I'm trying to write VBA to change the CommandText property of each query in a Workbook, but I'm having some trouble. I'm using Excel 2010.
At first, I had 5 Worksheets and each one had a single PivotTable with its own PivotCache. 5 Worksheets, 5 ODBC connections, 5 PivotCaches, 5 PivotTables. I had a solution written that worked fine. It looped through all Worksheets in the Workbook, then looped through all PivotTables in each Worksheet, and then changed the CommandText property for each PivotTable. The queries changed, the data refreshed, all good.
But then we wanted to add more PivotTables, but PivotTables that are using reusing some of these already-present queries: Some of the data we want to pivot 3 or 4 ways, but we want these 3 or 4 ways to share source data (as well as calculated fields). Easy, right? Just have the PivotTables share a PivotCache. We get our additional views of the data without bloating the filesize (already ~100 MB) or hammering the database.
But with shared PivotCaches, it doesn't make sense to loop through and alter PivotTables like I was doing before. That would, if I understand correctly, result in data being refreshed more than once for the shared PivotCaches. So, I read a bit more (I'm learning VBA as I go but have other programming experience) and realized there's a Workbook.PivotCaches collection; "Aha!", I thought, "I'll just loop through that instead of each PivotTable in each Worksheet."
But it doesn't work. It breaks when (I think) it tries to alter the CommandText property of a shared PivotCache (Run-time error '1004', Application-defined or object-defined error). Also (and I'm not sure exactly why this is happening) instead of modifying the existing PivotCaches it seems to be creating copies of them--after the script runs there are a handful of new data connections named Connection, Connection2, etc. It didn't do this before, when I was modifying PivotTable.CommandText instead of PivotCache.CommandText. [Edit: I think I got this wrong, it must have been PivotTable.PivotCache.CommandText, but I'd have to pull the old version out of git to check.]
I hope that explains what I'm trying to do and where I'm stuck; please ask if I left out something. So, anyone know a good way to change the CommandText property of a shared PivotCache? (And for bonus points, does anyone know why Excel is creating new connections when I try to modify PivotCache.CommandText?)
Thank you!
At first, I had 5 Worksheets and each one had a single PivotTable with its own PivotCache. 5 Worksheets, 5 ODBC connections, 5 PivotCaches, 5 PivotTables. I had a solution written that worked fine. It looped through all Worksheets in the Workbook, then looped through all PivotTables in each Worksheet, and then changed the CommandText property for each PivotTable. The queries changed, the data refreshed, all good.
But then we wanted to add more PivotTables, but PivotTables that are using reusing some of these already-present queries: Some of the data we want to pivot 3 or 4 ways, but we want these 3 or 4 ways to share source data (as well as calculated fields). Easy, right? Just have the PivotTables share a PivotCache. We get our additional views of the data without bloating the filesize (already ~100 MB) or hammering the database.
But with shared PivotCaches, it doesn't make sense to loop through and alter PivotTables like I was doing before. That would, if I understand correctly, result in data being refreshed more than once for the shared PivotCaches. So, I read a bit more (I'm learning VBA as I go but have other programming experience) and realized there's a Workbook.PivotCaches collection; "Aha!", I thought, "I'll just loop through that instead of each PivotTable in each Worksheet."
But it doesn't work. It breaks when (I think) it tries to alter the CommandText property of a shared PivotCache (Run-time error '1004', Application-defined or object-defined error). Also (and I'm not sure exactly why this is happening) instead of modifying the existing PivotCaches it seems to be creating copies of them--after the script runs there are a handful of new data connections named Connection, Connection2, etc. It didn't do this before, when I was modifying PivotTable.CommandText instead of PivotCache.CommandText. [Edit: I think I got this wrong, it must have been PivotTable.PivotCache.CommandText, but I'd have to pull the old version out of git to check.]
I hope that explains what I'm trying to do and where I'm stuck; please ask if I left out something. So, anyone know a good way to change the CommandText property of a shared PivotCache? (And for bonus points, does anyone know why Excel is creating new connections when I try to modify PivotCache.CommandText?)
Thank you!