Share via

How to change the sources of all Pivot tables in a workbook to a named data range?

Anonymous
2012-06-21T17:06:21+00:00

As the heading suggests this is what I'm attempting to do, I have about 30-40 pivot tables, all PT's (save 3) share the same data source I had them coded with specific ranges, but now I'm trying to make the range dynamic, accounting for future expansion of the data set (when more rows are added).

My current VBA code is below - the line in bold is where the code breaks with the error mentioned. Any ideas why?

Sub update_source()

Dim wb As Workbook

Dim PC As PivotCache

Dim ws As Worksheet

Dim rng As Range

openFile (RPL) ' This is calling another subroutine to open the files, works

Set wb = ActiveWorkbook

Sheets("Data").Select

Set rng = Range("A1")

Set rng = Range(rng, rng.End(xlDown))

Set rng = Range(rng, rng.End(xlToRight))

wb.Names.Add Name:="Data1", RefersTo:=rng

Sheets("Avail Data").Select

Set rng = Range("A1")

Set rng = Range(rng, rng.End(xlDown))

Set rng = Range(rng, rng.End(xlToRight))

wb.Names.Add Name:="Data2", RefersTo:=rng

For Each PC In wb.PivotCaches

PC.SourceData = "=Data1"

Next PC

End Sub

PC.SourceData = "=Data1" - this is where the code breaks, with run time error 1004: Application defined or Object defined error.


I have tried to refer to the named range as both this formula and also as Range("Data1") - both does not work.

"Data1" as you can see is defined above, I have tried defining the ranges before running this code to see if that fixes it. But no luck.

All help is welcome, I have no idea why this is happenign.

Thank You - Abhi

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

13 answers

Sort by: Most helpful
  1. Anonymous
    2012-06-21T19:01:51+00:00

    Abhi,

    Ok, looking a bit more closely at the code snips that I commented out ... You're creating some named ranges but that in itself isn't converting them to Excel Tables. You need to add something along the lines of

    ActiveSheet.ListObjects.Add(xlSrcRange, Range(rng), , xlYes).Name = "Data1"

    before you can use the "Data1[#All]" ref.

    ___________

    Regards, Tom

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-06-21T18:38:38+00:00

    Nope that is all of the code.

    Yes, it is still breaking at the same line, with the same error.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-06-21T18:29:57+00:00

    Abhi,

    Is it still breaking at the same line? I tested this with all of the lines between

    Set wb = ActiveWorkbook

    and

    For Each PC In wb.PivotCaches

    commented out, and it worked ok, based on a brief test where I had made an exact copy of a an ordinary table of data, and converted it to an Excel Table, and renamed it Data1.

    Is there more code after the bit you posted?

    ___________

    Regards, Tom

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-06-21T18:18:20+00:00

    Thank you for the reply.

    But that does not work either , gives me the same error.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2012-06-21T17:43:22+00:00

    Abhi,

    If your Table has been named Data1 then try this line instead:

    PC.SourceData = "Data1[#All]"

    ___________

    Regards, Tom

    Was this answer helpful?

    0 comments No comments