Using Excel As Your Database

Share

Facebook
Twitter
LinkedIn

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:

Using Excel as your Database - demo

Before we begin:

  1. 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.
  2. This has nothing related with the in-built Table (2007 and greater) / List (2003 and previous) feature of Excel.
  3. 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.

Screen Design

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.

VBA References

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.

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

30 Responses to “Great News: Chandoo becomes MVP”

  1. arkangel says:

    Congratulations! It's well deserved. 🙂

  2. Bhars says:

    This is amazing. Hearty congratulations and a rocking new year ahead!!!

  3. Frederick says:

    Congrats! I have learnt alot from your site... and the most important is learning how to achieve the most with the simplest concepts.

  4. Harish Chotai says:

    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.

  5. Pankaj Verm says:

    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

  6. Amit says:

    Congrats dude... fantastic news!

  7. Rich says:

    congratulations! your site is great, this is well deserved

    Rich

  8. Arnold says:

    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

  9. Jorge Camoes says:

    Congratulations, Chandoo! That's a great way to start the year and make the PHD even better.

  10. Jiri Cihar says:

    Congratulations, Chandoo.

    Your site is one of most useful on the net. Happy new year and lot of ideas you will present for us.

  11. Amit Agarwal says:

    Congrats.

    Just read your name in an email from Abhishek. Well deserved.

  12. Jon Peltier says:

    Congratulations, and Happy New Year.

  13. Fchaves says:

    Greetings from Rio de Janeiro my friend! You trully deserve it!

    Nive way to start 2009! Keep up the good work!

    FC

  14. Santosh says:

    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.

  15. Ketan says:

    @Hey Chandoo ! Congrates....

    Ab to treat mangta hai !

  16. Rajiv Thakur says:

    Well deserved Chandoo!!

  17. Cheryl says:

    Congrat's!! Very well deserved 🙂

  18. azmat says:

    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..........

  19. Chandoo says:

    @All: thanks everyone 🙂

  20. JP says:

    Congratulations Chandoo, nice job!

  21. Subbaraman says:

    Chandoo,

    A well deserved recognition and a good start to the New Year. Continue your good work.

    Subbu

  22. Vishal Haria says:

    Many Congratulations.
    You deserve a Ph.D. 🙂

  23. Govar says:

    Congrats dude. Rock on!

  24. [...] 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 [...]

  25. 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.

  26. Jon Peltier says:

    Dipak -

    The first thing you need to cultivate is the ability to search the web effectively. You could start by Googling "Microsoft MVP". 

  27. [...] 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 [...]

  28. Sumit says:

    Congrats Chandoo!!

Leave a Reply