Use Excel VBA to change Powerpivot connection to csv file - vba

How can I use vba to modify a Powerpivot connection to a csv file? I have an Excel workbook with a Powerpivot model that connects to a csv file. In the Powerpivot window, I can navigate to the Home->Existing Connections and edit the file path as desired. I can't get close to editing this in vba.
I found this link on parameterizing Powerpivot connections.
Unfortunately, I couldn't figure out how to modify to apply to csv file connection rather than database connection.
An alternate approach is described on this page. This approach creates a connection to the data in Excel itself. The connection is then available in Powerpivot. It is critical to not create the connection in Powerpivot bc the author says connections created in Powerpivot cannot be modified by vba.
I can create this connection manually through the
Data->Get External Data->From Text menu option.
I can create the same connection in vba using "Workbooks("myWorkbook.xlsm").Connections.AddFromFile "data.csv". I can manually edit this connection in the Data->Connections->Connections dialog. However, when I try to set this in vba using the WorkbookConnections.TextConnection property, vba says "Object doesn't support this property or method".
The above link focuses on database connections rather than csv file connections. However, it seems possible to set up the csv file with an
Microsoft ACE OLEDB 12.0 connection, but I can't quite grasp it.
There are similar questions on SO that address vba modifying pivot sources (like here:Changing pivot table external data source path with Excel macro).
However, I don't want to bring the data into a table in Excel, I want to connect to it so I can work with it in Powerpivot.
I could probably do this with Power Query, but since I already have the csv in the desired format, I'd rather not have to introduce this additional step.
I'm using Excel 2013 64 bit on Windows 7. Thank you for your help.

I've been playing around with something similar. Haven't succeeded yet, but maybe we can figure it out together...
One thing I've noticed is that it looks like these connections to CSV files aren't actually TextConnections like you'd expect, but rather OLEDBConnections.
EDIT: According to https://goo.gl/x17Nuj, it's just not possible:
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.
(Emphasis is mine)

I was able to modify the file location by setting up the csv file connection as a Microsoft.ACE.OLEDB.12.0 connection. It's actually labelled "Microsoft Office 12.0 Access Database Engine OLE DB Provider" in the Data Connection Wizard. This link helped me figure it out. You need to have "Microsoft ActiveX Data Objects 6.1" added as a reference. Once the connection was created manually, here's the code I used to modify the connection:
Sub editConnection()
With ActiveWorkbook.Connections("myConnectionName").OLEDBConnection
.Connection = "OLEDB;Provider= Microsoft.ACE.OLEDB.12.0; " & _
"Data Source=\\something.com\shared\myDepartment\newDirectory; " & _
"Mode=Read;Extended Properties = ""Text;HDR=Yes;FMT=Delimited;"""
End With
ActiveWorkbook.Connections("myConnectionName").Refresh
End Sub
I didn't figure out how to modify the filename. This also forces the table name in PowerPivot to be the name of the csv file. You can't edit that without breaking the connection.

Related

Shared Data Source for Excel

I am currently creating reporting within excel for lets say 'Non Advanced Users'. I Have successfully created this in and I can execute the stored procedure with parameters typed into excel cells. But when I send this to another user it is saying that it is unable to connect to the data source. Is there a way I could tell excel to use a data source on our network. The connection has SQL Authentication. Basically I want to try and avoid having to create data sources via excel on 100+ machines!!
Yes you can. You can save the connection setting in the excel file itself, and just send out that file. See picture below for step-by-step guide.
Remember to CLEAR (uncheck) the 'Always Use Connection File' in Step 6.

Link Data Models to multiple files

I created a file that imports multiple tables, and then I do several modifications and calculations with Power Pivot. The problem is that I want to use the final results, not only in that current workbook, but also in other workbooks.
So my question is, is there a way to link other excel files to that data model that I created and worked on?
Please note the data changes every day, so I am looking for a permanent connection.
Appreciate any help here.
if I understand you correctly you would like to access the PowerPivot model/measures that you have created in one xls file from within other xls files.
If so, the "core" vs. "thin" workbook approach might be what you are looking for. This approach works with Excel 2010 but not with Excel 2013 however and you will need a Sharepoint Server with PowerPivot for this to work.
The approach is described in Rob Collie's awesome PowerPivot blog:
http://www.powerpivotpro.com/2011/02/powerpivot-scheduled-refresh-pt-3-thin-workbooks/
and here:
https://pivotstreamllc.zendesk.com/hc/en-us/articles/201148566-Splitting-Workbooks-into-Core-Thin-Pairs
another option is to load all your data source in one Master workbook, then write back the result in the same workbook using Reverse Linked Tables.
Now you can use that Master workbook as your backend for all your reports.
or use PowerBI Desktop as your backend, officially it is not support but Excel can have a local connection to PowerBI desktop.
I put more details here
https://datamonkeysite.com/2016/11/13/thin-and-core-workbook-without-sharepoint-or-ssas/

xlsm into sql server

I have an xlsm file with macros that enable data collection from OLE DB source. I just need to make an import package with it in sql server. But when I'm trying to use import/export wizard or ssis-it shows that it can not deal with .xlsm extentions.
Is there any way to deal with it?
follow this :
1-Create an Excel Connection Manager to refer to ANY other spreadsheet with a "valid" XLS or XLSX extension. (You don't have to save your XLSM file "as" an XLSX.)
2-Select the Excel Connection Manager in your Connection Managers pane.
3-Open the Properties Window (F4).
4-Change the "Server Name" property to the full pathname of your XLSM file.
note: check the file should not be in read only mode
Had the same issue (SQL Server 2008 R2), Rahul's solution worked for me, but I just want to add a couple of things:
Select Excel 2007 in Excel version when creating Excel Connection.
Use SQL Command as Data access mode when editing the Excel Source. Visual Studio crashed for me every time I was trying to use Table or view.

How to extract data from a database and populate a sheet in Excel

I am storing data in a backend database (PostgreSQL) which is running on a Linux machine. I want to be able to fetch data from the database, and populate a sheet in an excel workbook, so that I can carry out analysis in Excel.
It has been quite a while since I wrote anything in VBA, so I would appreciate some help (or links) in getting started. I would like to know the best way to approach this:
Pure VBA solution OR
Mixture of C# or other .Net language for data extraction logic and VBA for manipulating Excel objects (sheet data population etc)
Any ideas, tips, snippets and/or links that can help me get started on the twin objectives:
fetching data from a backend database (PostgreSQL) into Excel
populating a specified sheet in Excel with the columnar data retreived from the database
will be much appreciated.
If you are just trying to import data, Excel can do that without additional code. Just set up your windows client to connect to your postgreSQL server thru ODBC. (Here's one way guide to setting that part up: enter link description here
Then in Excel (Use a modern version, like 2007 or greater) from the "Data" tab, click on "Existing Connections" to connect to the ODBC connection you set up and pick the tables/data to import into excel. Once the spreadsheet has loaded the data, you can just click the "Refresh All" button to update the data.

Writing data back to SQL from Excel sheet

I know it is possible to get data from a SQL database into an excel sheet, but i'm looking for a way to make it possible to edit the data in excel, and after editing, writing it back to the SQL database.
It appears this is not a function in excel, and google didn't come up with much usefull.
If you want to have the Excel file do all of the work (retrieve from DB; manipulate; update DB) then you could look at ActiveX Data Objects (ADO). You can get an overview at:
http://msdn.microsoft.com/en-us/library/ms680928(VS.85).aspx
You want the Import/Export wizard in SQL Management Studio. Depending on which version of SQL Server you are using, open SSMS (connect to the SQL instance you desire), right click on the database you want to import into and select Tasks.. "Import Data".
In the wizard, click Next (past the intro screen) and from the Data Source drop list select "Microsoft Excel". You specify the path and file name of the Excel spreadsheet, whether you have column headings or not.. then press Next. Just follow the wizard through, it'll set up the destination (can be SQL Server or another destination) etc.
There is help available for this process in SQL Server Books Online and more (a walkthrough) from MSDN.
If you need something deployable/more robust (or less wizard driven) then you'd need to take a look at SQL Server Integration Services (for a more "Enterprise" and security conscious approach). It's probably overkill for what you want to accomplish though.
There is a new Excel plug-in named "MySQL for Excel" : http://www.mysql.com/why-mysql/windows/
I just had a need to do this, and this thread has been quiet for a long time, so I thought it might be useful to supply a recent data point.
In my application roving salespeople use a copy of an Excel workbook that tracks the progress of a prospect through a loan application. The current stage of the application needs to be automatically saved back to a remote SQL database so that we can run reporting on it.
Rejected methods for updating the database from Excel:
SSIS and OpenRowSet are both methods for allowing SQL Server to pull the data from Excel, and don't work very well when the Excel workbook is sitting in an undefined location on a user's computer, and certainly not when the workbook is currently open in Excel.
ADO is now, if not actually deprecated, nevertheless looking very long in the tooth. Also, I wanted the solution to be robust in the face of the user possibly not being connected to the internet.
I also considered running a web API on the destination server. Macros in the Excel workbook connect to the web API to transfer data. However, it can sometimes be painful to allow a web API to talk to the outside world. Also, the code to make it robust in the face of temporary loss of internet connection is painful.
The adopted solution:
The solution I plan to adopt is low-tech: email. Excel emails the data to an address hosted on an Exchange server. Everyone in the company has Outlook installed, so the emails are sent by programmatically adding them to the Outlook Outbox. Outlook nicely handles the case when the user is offline. At the server end, a custom C# executable, fired up at regular intervals by the Task Scheduler, polls the inbox and processes the emails.
You could use try these add-ins :
www.QueryCell.com (I created this one)
www.SQLDrill.com
www.Excel-DB.net
You can use the OPENROWSET function to manipulate Excel data from a T-SQL script. Example usage would be:
UPDATE OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;DATABASE=c:\MySpreadsheet.xls',
'Select * from MyTable')
SET Field1='Value1' WHERE Field2 = 'Value2'