Export data from SQL to Excel spreadsheet using VBA

Yodalearning>Tutorials>Export data from SQL to Excel spreadsheet using VBA

In this blog, we will Import data from SQL to Excel spreadsheet using VBA. Assuming that you have a little bit of knowledge on SQL server. Now if you are preparing reports in Excel and the data is on your SQL server then you don’t need to copy the data first from SQL server to prepare the report.

Now if your manager needs the report very frequently then you need to do the same task again and again. Frankly speaking, I faced this kind of situation in my previous company and I prepared the report using VBA and handed over the file to my manager. Now, whenever he will click the button he will get the report of the LIVE DATA from SQL to Excel.

Export data from SQL to Excel spreadsheet using VBA

In this example, I am using Microsoft Sample Database Adventureworks2012

Just open a dummy excel workbook. Create the dummy button like below:

export data from sql to excelThe buttons are in the above picture are basic shapes. And “HP-PC” is the system name. If you open the file in your system, it will show your system name. And the time is coming from system’s time.

After creating the above button we will create the code. First, we are trying to focus on Sales Report. You need to decide what will be the TSQL code for this. For Example, we need to prepare a report which will show the Name of the person and from which territory they have sold in what amount. First of all, we need to check the TSQL code in SQL server. I have written a simple which will pull up the records of person name and territory name and the sum of sales amount of current year and the last year. Here is my query in SQL server.

export data from sql to excel using vbaIf I ran the above query in SQL Server I am getting 17 records.

export data from sql to excel using vba macrosNow our intention is something different. We want the same result in Excel. You can copy the result and paste it in Excel sheet that is fine. But if you need to do the same thing every day and then you can simply copy the below code and paste it on VBA page and then connect the code with the button to get data from SQL to Excel,

Please follow the below steps to get the result in Excel sheet:

  1. Press Alt + F11 to go the VBA page
  2. Paste the below code
  3. Press F5 or run the code

Read related article:  Merge Multiple Excel Workbooks into a Spreadsheet using Excel Macro


Dim oConn As ADODB.Connection

Dim rs As ADODB.Recordset

Dim fld As ADODB.Field

Dim mssql As String

Dim row As Integer

Dim Col As Integer

Dim ws As ThisWorkbook

Set ws = ThisWorkbook

Application.ScreenUpdating = False

Set oConn = New ADODB.Connection

Set rs = New ADODB.Recordset

mssql = “Select P.Firstname,P.lastname,st.name as Territoryname, ” & _

sum(s.salesYTD) as CurrentYearSales, & _

Sum(s.saleslastyear) As LastyearSales & _

from Sales.salesperson s & _

inner join person.person p on p.businessEntityid=s.businessEntityid & _

inner join sales.salesterritory st on st.territoryid=s.territoryid & _

group by P.Firstname,P.lastname,st.name

oConn.ConnectionString = “driver={SQL Server};” & _

server=HP;authenticateduser = TRUE;database=Adventureworks2008R2


‘======If you have then change your user name and password here=====


‘ oConn.ConnectionString = “driver={SQL Server};” & _




oConn.ConnectionTimeout = 30


rs.Open mssql, oConn

If rs.EOF Then

MsgBox “No matching records found.”




Exit Sub


End If


row = 5

Col = 1


For Each fld In rs.Fields

Sheet2.Cells(row, Col).Value = fld.Name

Col = Col + 1





row = row + 1


Do While Not rs.EOF


Col = 1


For Each fld In rs.Fields


Sheet2.Cells(row, Col).Value = fld


Col = Col + 1




row = row + 1








End Sub

Before running the code you need to change some setting. Go to tools on VBA page and then click on the reference and then select “Microsoft ActiveX Data Objects 2.8 Library and press OK.VBA-page-Reference-ActiveX-Data-ObjectsNow we all are good to go. Now press F5 to run the code. You will notice the same records are there in the excel sheet. Now if the data or any record is updated in the base table in SQL Server you will get the update result. Now all the time you will get the live data. That’s why it is powerful. You can send the file to your manager or boss. He or she will get the updated report all the time.

We need to assign the code to the button “Sales Report” we created earlier. Go to that button and then right-click on it and click on “Assign Macro

Sales-Report-Assign-Macro-SQL-to-Excel-4Then click on “New”. You will be redirected to the new VBA page. Just write “Call DataTakenFromSQLServer”. Finally, you are done. Whenever you click on Sales Report you will get the updated result.VBA-page-Call-DataTakenFromSQLServer-2Now Go back to your sheet. And Click on the “Sales Report”. You will notice that the data is coming in the Excel sheet. Another one thing you will have to be careful that the SQL server service is on the form where the data is coming.

Please download the workbook from here and check the code. Please change the SQL server name and database name to check the output of your query. You can also write your own query and check whether it is working or not. Let us know if you have any problem to understand the code. We will help you out.

Do feel free to write a comment below Discussion section, will answer as always.

2 Responses

Leave a Reply