Query-Import SharePoint List to Excel using Data Source Connections

Have you ever had to query and import SharePoint list to Excel? or ever required to use SharePoint list as excel data source? Well, just follow these steps to excel data connection to use SharePoint list as excel data source:

Step 1: Create a SharePoint list view with desired columns and order:
Go to your SharePoint list, Create a new view matching your requirement by adding necessary columns to it.

Step 2: Use Export to Excel to generate Data Source Connection
Navigate to your SharePoint list view created in Step-1, Use "Export to Excel" button under "List" tab. This imports SharePoint list to Excel and establishes data source connection in Excel as below.
use sharepoint list as excel datasource
 Make sure you set the below properties to skip column width and preserve sort/filter/layout/formats.
excel data connection to sharepoint list
You can also set the data refresh when opening the file.
sharepoint list as excel data source

How to Refresh the Data?
All you have to do is, either right click on any data imported and click "Refresh" in the context menu or Click on "Refresh All" button under "Data" tab of Excel.

Once you click refresh, Your Excel sheet will be synced from SharePoint list (One way - from SharePoint list!)
query sharepoint list using excel
Fix data format in Lookup and People picker columns
When you import data from SharePoint list to Excel, It just imports the data in the same format SharePoint stores internally. Say for E.g. You'll see John Mathew;#157;#Omar Saad in people picker column.

Lets create a custom function in VBA code to replace the format "Number#;" in People Picker, Lookup columns with regular expressions:
  • Go to Excel >> File >> Options >> Customize Ribbon >> Enable "Developer" check box under Customize the Ribbon. This adds a new tab "Developer" in the Ribbon. 
  • Click on "View Code" button under "Controls" group of Developer Tab. 
  • Click on Tools >> Add Reference >> Pick "" to add Regular Expressions library: Microsoft VBScript Regular Expressions 5.5
  • Right Click the VBAProject >> Insert >> Module. Insert the below code into the Module.
    Function xREPLACE(pattern As String, searchText As String, replacementText As String, Optional ignoreCase As Boolean = True) As String
        On Error Resume Next
        Dim RegEx As New RegExp
        RegEx.Global = True
        RegEx.MultiLine = True
        RegEx.pattern = pattern
        RegEx.ignoreCase = ignoreCase
        xREPLACE = RegEx.Replace(searchText, replacementText)
    End Function
  • Now, Create a new column in Excel, Say for E.g. You are going to properly format "Deal Lead" column, Enter the formula as:
    =xREPLACE("\#\d*;|#\d*|\d*;#",TRIM([Deal lead])," ")
  • You can hide the original column "Deal Lead" and Name your new columns similar to "Deal Lead", say "Deal leader" or add a . (dot) prefix.
  • Save the Excel file as "XLSM" format so that the code saved along with your Excel sheet.

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.