如何使用 vba 修改 Powerpivot 连接到 csv 文件?我有一个 Excel 工作簿,其 Powerpivot 模型连接到 csv 文件。在 Powerpivot 窗口中,我可以导航到 Home->Existing Connections 并根据需要编辑文件路径。我无法在 vba 中进行编辑。
不幸的是,我不知道如何修改以应用于 csv 文件连接而不是数据库连接。
我可以通过手动创建此连接
Data->Get External Data->From Text menu option
.我可以使用 "Workbooks("myWorkbook.xlsm").Connections.AddFromFile "data.csv"在 vba 中创建相同的连接。我可以在 Data->Connections->Connections 对话框中手动编辑此连接。但是,当我尝试要使用 WorkbookConnections.TextConnection 属性在 vba 中进行设置,vba 会说“对象不支持此属性或方法”。
上面的链接侧重于数据库连接而不是 csv 文件连接。但是,似乎可以使用
Microsoft ACE OLEDB 12.0 连接,但我不能完全掌握它。
SO上有类似的问题可以解决vba修改数据透视源的问题(例如:Changing pivot table external data source path with Excel macro)。
但是,我不想将数据放入 Excel 中的表中,我想连接到它以便可以在 Powerpivot 中使用它。
我可能可以使用 Power Query 执行此操作,但由于我已经拥有所需格式的 csv,我宁愿不必引入此额外步骤。
我在 Windows 7 上使用 Excel 2013 64 位。感谢您的帮助。
最佳答案
我一直在玩类似的东西。还没有成功,但也许我们可以一起解决......
我注意到的一件事是,这些与 CSV 文件的连接实际上并不是您所期望的 TextConnections,而是 OLEDBConnections。
编辑:根据 https://goo.gl/x17Nuj ,这是不可能的:
Once you modify the connection inside PowerPivot, the link between the Excel and the PowerPivot connections is broken. In fact, you can no longer modify the connection properties in Excel and, if you want to load another table, then you have to use the PowerPivot add-in. Needing to use the add-in means that the option of modifying the connection is no longer available in VBA Because (as you have seen) there is no way to modify the PowerPivot connections using VBA.
(重点是我的)
关于vba - 使用 Excel VBA 将 Powerpivot 连接更改为 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900967/