Dynamically Passing Parameters to a SQL Stored Procedure from PowerPivot Using VBA

Update September 2022:

I wrote a new blog post covering the same scenario in Excel 365.

In this post, I express an easy way to refresh a PowerPivot model dynamically based on SQL Server Stored Procedures. Let’s start with SQL Server Management Studio (SSMS) 2012 and use Adventure Works 2012 database. Run the following script to see the results in SSMS:

exec [dbo].[uspGetBillOfMaterials] 727, ‘2009-01-02’

First parameter: Start Product ID

Second Parameter: Check Date

Results:

Now we want to see the results for the following script:

exec [dbo].[uspGetBillOfMaterials] 762, ‘2009-01-02’

 Results:

Now we want to do the same with PowerPivot.

·         Open Microsoft Excel, I’m using Excel 2013, then go to PowerPivot tab in the ribbon and click “Manage”

·         Click “Get External Data”

·         Click “From Database”

·         Click from SQL Server

·         Type a friendly name for the connection, type a server name and database name then click Next

·         Select “Write the query that will specify the data to import” then click Next

·         Type a friendly name for the query and put the following script in the SQL Statement text box then click Finish

exec [dbo].[uspGetBillOfMaterials] 727, ‘2009-01-02’

·         As you can see 24 rows are transferred

·         Now you can see the results in PowerPivot

·         Close PowerPivot and go the data tab in the Excel ribbon then click “Connections” from connections section

·         Select SP_Connection1 then click Properties

·         Go to “Definition” tab. As you can see the connection is a read-only connection and we are unable to change the command text

·         The sad news is that the connection is read-only forever and we are unable to modify it from VBA.

·         Click OK to close the connection properties window. DO NOT close the “Workbook Connections” window

·         Select the SP_Connection1 again and this time click the “Add…” button and the click “Add to the Data Model”

·         Go to the “Tables” tab and select “PowerPivot_SP” then click “Open”

·         This will add a new connection to the workbook with the same settings

·         Select the new connection that is automatically named “SP_Connection11” then click the “Properties” button to rename the connection to SP_Connection2

·         Change the connection name to SP_Connection2. If you click on the “Definition” tab then you can see that the new connection is NOT read-only, so we’ll be able to modify it using VBA.

·          Click OK to close the connection properties. Now the connection is renamed to SP_Connection2.

·         Go to PowerPivot again to check what we have in the model now

·         As you see there is a new table added to the model with the same results as the “PowerPivot_SP” table

NOTE: DO NOT RENAME THE TABLE OR ANY OF THE COLUMNS. IF YOU DO SO, THE NEW CONNECTION WILL GET READ-ONLY AND YOU’LL BE UNABLE TO CHANGE THE SQL QUERY ANYMORE.

·         Now press “Alt+F11” to open visual basic for Excel

·         From “Microsoft Excel Objects” double click on “ThisWorkbook”

·         From the objects drop down select “Workbook” and the select “SheetChange” procedure

·         Copy and paste the following VBA scripts to change the SP_Connection2 dynamically. The values of A2 and B2 cells will be passed to the SQL Server stored procedure and the connection will be refreshed to fetch the results from SQL Server:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

 If Intersect(Target, Range(“B2”)) Is Nothing Then

  ‘Nothing

  Else

  ActiveWorkbook.Connections(“SP_Connection2”).OLEDBConnection.CommandText = “exec [dbo].[uspGetBillOfMaterials] ‘” & Range(“A2”).Value & “‘, ‘” & Range(“B2”).Value & “‘”

  ActiveWorkbook.Connections(“SP_Connection2”).Refresh

  MsgBox (“ConMod: ” & ActiveWorkbook.Connections(“SP_Connection2”).OLEDBConnection.CommandText)

  End If

NOTE: The above code will refresh the PowerPivot data whenever you modify the value of the B2 cell in the current active worksheet. You can remove the MsgBox line from the code. I’ve put this part for testing purposes.

·         Press “Alt+F11” again to go back to Excel and put 762 in the A2 cell and 2009-01-02 in the B2 cell and press Enter

·         As you can see the message box shows us that the SP_Connection2 is changed based on the values of cells A2 and B2.

·         Open PowerPivot again to check if the data are loaded to the model correctly

·         Finally we should save the file. Select “Excel Macro-Enabled Workbook (*.xlsm)” from the save as type, otherwise you’ll get the following message

You can now delete the PowerPivot_SP table from the PowerPivot model.

Now you can insert the pivot table in excel, modify the values of A2 and B2 cells and the pivot table will automatically refresh.

Here is the results of the stored procedure with different values for the parameters:

Start Product ID = 727

Check Date = 2009-01-02

Start Product ID = 762

Check Date = 2009-01-02

We’re done now.

Maybe some of you guys think that it’s really a pain that you cannot rename the table and it’s columns from PowerPivot. Well, I should say that I do agree with you. But, sadly, it is how it works for now. I’ve done lots of investigations to find a way to modify the SQL Statement in “Edit Table Properties” from PowerPivot, but, it seems it is untouchable trough VBA. You can find the “Edit Table Properties” from PowerPivot, Design tab then click on “Table Properties”.