Cannot open PivotTable source file

只谈情不闲聊 提交于 2019-12-12 17:34:13

问题


Answered

I recorded an Excel macro (and saved it in my personal macro workbook) to create a pivot table and was hoping that I would be able to use it on multiple worksheets. The code works fine when I run it on spreadsheets on my computer, but will not run on other computers or if I try to run it in the VBA editor. It keeps giving the following error: http://imgur.com/xNU1R2F (Cannot find PivotTable source file)

The thing is, the file is saved in that location under the same exact name. I have tried replacing the path with the range of the spreadsheet (which isn't ideal if I want to use it on other spreadsheets), but that produced an application-defined or object-defined error. I have also tried putting it in different locations to no avail.

The code I'm using is as follows:

   Sheets.Add
   ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "File_Name!R1C1:R1681C105", Version:=xlPivotTableVersion14). _
        CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
        , DefaultVersion:=xlPivotTableVersion14

I think the problem is within "File_Name!R1C1:R1681C105" (the path does change if I change the file location, but produces the same error)

How can I get my code to find the source file?

Answer: munircontractor's method worked. Had to add the following code just before my code and it was then able to find the source file.

Set SrcWbk = Workbooks.Open("File_Name", , ReadOnly)

While it works fine on my computer, I found out that in order for my code to run on other computers, the worksheet cannot be open in Excel, it will only run the macro on a blank worksheet. Could just be a fluke, but wanted to mention it in case anyone else experiences the same problem.

来源:https://stackoverflow.com/questions/33375792/cannot-open-pivottable-source-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!