MSSQL – Export data from SQL Server to Excel with VBA

So, after importing data to SQL Server from Excel now it is time for the reversing policy. Actually, much of the code was available in the Microsoft site here, but still, it was not all that I needed. I wanted to make it a little nicer, with headers.

my_db

Thus for the headers, it was really a 15 minute challenge, until I found the way to take them and write them at the excel sheet. Thus, at the end I had something workable.

Let’s see the code here:

What the code does? Pretty much it deletes anything you have in the active sheet, then writes the headers through a separate recordset called rdHeaders. Then it writes the rest table. As far as I am using early binding, the code would not work, if you do not add MS ActiveX Data Objects 2.8 Library to the References from Tools>References in the Visual Basic Editor. The differences between the early and the late biding is explained here.

The code is also available in GitHub here.

That’s all! Enjoy it! 🙂

Tagged with: ,