This is a guest post by Vijay, our in-house VBA Expert.
Often I have thought, if I could have write “Select EmployeeName From Sheet Where EmployeeID=123” and use this on my excel sheet, my life would be simpler. So today we will learn how to do this.
People spend a lot of time thinking whether to use Excel as their database or not. Eventually they start using Access or SQL Server etc.
Today we will learn how to use Excel as a Database and how to use SQL statements to get what we want.
Excel as a Database – Demo
We will learn how to build this:

Before we begin:
- The entire sheet (where the raw data has been stored) will be referred as one single database table by Excel. This is very important to understand.
- This has nothing related with the in-built Table (2007 and greater) / List (2003 and previous) feature of Excel.
- If you know SQL (Structured Query Language) your task becomes much easier.
Setting up Excel as Database
We need some raw data and we will utilize Customer Service Dashboard sample database here.
Let’s get started.
First we will design the structure of what all option we want to present for filtering the data, which you can see in the interface below.
Once the user clicks on Show Data we will use a SQL statement to filter-out the data as per the drop down options selected by the user and the put them in the table below.
We will also use another SQL statement to populate the top right hand side table for calls data when all the 3 drop downs have some options selected.

Adding Active-x data objects reference
We need to add a reference to the Microsoft ActiveX Data Objects Library to be able to use the worksheet as a database table. You can do this from Visual Basic Editor > Tools.

I usually select the most recent version, however if you are developing a product it will be best suited if you are familiar with the operating system and office version used by the end-user’s system and accordingly select the best version available.
Opening Excel as a Database
Once this is done we need to hit the road with some VBA code.
Public Sub OpenDB()
If cnn.State = adStateOpen Then cnn.Close
cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
cnn.Open
End Sub
The above procedure is the heart of this post, this is where we define how to use the current Excel workbook as our database.
cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
On this line, we define all the possible file extensions that we are allowed to create an Excel Workbook and then use as our database.
Let’s understand the code module
When you click on the Update Drop Downs button, the VBA code uses the “Data” worksheet as a table and then finds unique values for Products, Region and Customer Types and then populates them as ListItems for the ComboBox controls.
Example for Products drop down
strSQL = "Select Distinct [Product] From [data$] Order by [Product]"
closeRS
OpenDB
cmbProducts.Clear
rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
Do While Not rs.EOF
cmbProducts.AddItem rs.Fields(0)
rs.MoveNext
Loop
Else
MsgBox "I was not able to find any unique Products.", vbCritical + vbOKOnly
Exit Sub
End If
What is important to notice here is how the Table and Fields have been identified using square brackets unlike traditional SQL where we just provide the name, also the table name has to be suffixed with a $ symbol at the end.
As I have suggested earlier, one entire sheet will be treated as one single table, so if you have multiple datasets that were currently organized within one sheet you may have to create multiple sheets to store that data to be able to use them as tables. This would also make maintenance of data easier.
Using Excel SQL to consolidate two sheets in to one
Many people ask, how to consolidate 2 or more sheets which have the similar data. Well I would have adopted this method and wrote a simple query as below.
SELECT ID, FirstName, MiddleName, LastName, Age, DOB From [Table1$]
UNION ALL
SELECT ID, FirstName, MiddleName, LastName, Age, DOB From [Table2$]
This would allow me to use both the sheets as one table and fetch all of my data into a new sheet.
Download Excel As Database Demo File
Click here to download the demo file & use it to understand this technique.
Do you use Excel as a database?
Do you also user Excel as your database? If yes please put in the comment below how do you use the same and what has been your experience. Leave a comment.
More on VBA & Macros
If you are new to VBA, Excel macros, go thru these links to learn more.
Join our VBA Classes
If you want to learn how to develop applications like these and more, please consider joining our VBA Classes. It is a step-by-step program designed to teach you all concepts of VBA so that you can automate & simplify your work.
Click here to learn more about VBA Classes & join us.
About Vijay
Vijay (many of you know him from VBA Classes), joined chandoo.org full-time this February. He will be writing more often on using VBA, data analysis on our blog. Also, Vijay will be helping us with consulting & training programs. You can email Vijay at sharma.vijay1 @ gmail.com. If you like this post, say thanks to Vijay.

















30 Responses to “Great News: Chandoo becomes MVP”
Congratulations! It's well deserved. 🙂
This is amazing. Hearty congratulations and a rocking new year ahead!!!
Congrats! I have learnt alot from your site... and the most important is learning how to achieve the most with the simplest concepts.
Thanks for the excel calendar. Is it possible to get a simple big fonted calendar printable on an A4 size paper without any distracive notes or visuals? BTW, I have already signed up for your newsletter. With warm regards and
Gratefully yours
50+ year old CHarish.
Hey Chandoo,
Great to hear that. Congratulations! The best new year gift, I would say. Keep it up, u've been doing extraordinary work for the excel users community.
Regards,
Pankaj Verma
Congrats dude... fantastic news!
congratulations! your site is great, this is well deserved
Rich
I recently found your site, I visit many. The tips that you provide are in the top 1% of all the sites I visit. Keep on Excelling.
Arnold
South Africa
Congratulations, Chandoo! That's a great way to start the year and make the PHD even better.
Congratulations, Chandoo.
Your site is one of most useful on the net. Happy new year and lot of ideas you will present for us.
Congrats.
Just read your name in an email from Abhishek. Well deserved.
Congratulations, and Happy New Year.
Greetings from Rio de Janeiro my friend! You trully deserve it!
Nive way to start 2009! Keep up the good work!
FC
That was quite forseeable , so you have now really got your PhD in excel.
Anyways Chandoo you have made excel a real Fun doo
I will like you to write some more on INDEX and MATCH function in near future.
@Hey Chandoo ! Congrates....
Ab to treat mangta hai !
Well deserved Chandoo!!
Congrat's!! Very well deserved 🙂
i always browsed mr.excel and used to see MVP writtne below names of people who used to solve queries in excel forum there......i just used to admire as to what they have special in them that they are MVP......
but now i got my answer...............u deserve it man..........
@All: thanks everyone 🙂
Congratulations Chandoo, nice job!
Chandoo,
A well deserved recognition and a good start to the New Year. Continue your good work.
Subbu
Many Congratulations.
You deserve a Ph.D. 🙂
congrats.....
Congrats dude. Rock on!
[...] charting community in 2007 and has been growing strongly ever since. In year 2009, I have received the MVP award from Microsoft. Just few days back I have become a dad [...]
Respected sir,
I am impressed!.... Good job done.. Keep it up...
Sir, How to be a MVP certified person. What level of knowledge is required for it? send me links if possible.
Please reply...
Regards,
Dipak Khalasi.
Dipak -
The first thing you need to cultivate is the ability to search the web effectively. You could start by Googling "Microsoft MVP".
[...] boy and girl which has been made hectic and incredibly fun ever since to their life.He has been awarded MVP status in 2009 by Microsoft(and renewed in 2010,2011 & 2012).His MVP profile is here.If you want to contact him direct then [...]
Congrats Chandoo!!
[…] Chandoo becomes MVP […]