MySQL Tutorial - K
MySQL Tutorial - K
You can use Microsoft Query in Excel to retrieve data from an Excel Workbook as well as
External Data Sources using SQL SELECT Statements. Excel Queries created this way can
be refreshed and rerun making them a comfortable and efficient tool in Excel.
Microsoft Query allows you use SQL directly in Microsoft Excel, treating Sheets as tables
against which you can run Select statements with JOINs, UNIONs and more. Often Microsoft
Query statements will be more efficient than Excel formulas or a VBA Macro. A Microsoft
Query (aka MS Query, aka Excel Query) is in fact an SQL SELECT Statement. Excel as well as
Access use Windows ACE.OLEDB or JET.OLEDB providers to run queries. Its an incredible
often untapped tool underestimated by many users!
Using MS Query in Excel you can extract data from various sources such
as:
ExcelFiles – you can extract data from External Excel files as well as run a SELECT
query on your current Workbook
MS SQL Server – you can extract data from Microsoft SQL Server Tables
CSV and Text – you can upload CSV or tabular Text files
I will extract data from an External Excel file called MOCK DATA.xlsx. In this file I have a list
of Male/Female mock-up customers. I will want to create a simple query to calculate how many
are Male and how many Female.
1. Return Data to Microsoft Excel – this will return your query results to Excel and complete the
Wizard
2. View data or edit query in Microsoft Query – this will open the Microsoft Query window and
allow you to modify you Microsoft Query
Import Data
When you are done modifying your SQL statement (as I in previous step). Click the Return
data button in the Microsoft Query window.
This should open the Import Data window which allows you to select when the data is to be
dumped.
AS you can see there are quite a lot of steps needed to achieve something potentially pretty
simple. Hence there are a couple of alternatives thanks to the power of VBA Macro….
1 Sub ExecuteSQL()
'AnalystCave.com
3
On Error GoTo ErrorHandl
4
Dim SQL As String, sConn As String, qt As QueryTable
5
SQL = InputBox("Provide your SQL Query", "Run SQL Query")
6
If SQL = vbNullString Then Exit Sub
7 sConn = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;;Password=;User ID=Admin;Data So
8 ThisWorkbook.Path & "/" & ThisWorkbook.Name & ";" & _
11 With qt
End Sub
20
21
Just create a New VBA Module and paste the code above. You can run it hitting
the CTRL+SHIFT+S Keyboardshortcut or Add the Macro to your Quick Access Toolbar.
ALTERNATIVES IN EXCEL –
POWER QUERY
Another way to run queries is to use Microsoft Power Query (also known in Excel 2016 and up
as Get and Transform). The AddIn provided by Microsoft does require knowledge of the SQL
Language, rather allowing you to click your way through the data you want to tranform.
Microsoft Queries are more efficient when you know SQL. While you can click your
way through to Transform Data via Power Query someone who knows SQL will likely
be much quicker in writing a suitable SELECT query
You can’t re-run Power Queries without the AddIn. While this obviously will be a less
valid statement probably in a couple of years (in newer Excel versions), currently if
you don’t have the AddIn you won’t be able to edit or re-run Queries created in Power
Query
MS Query Cons: Microsoft Query falls short of the Power Query AddIn in some other
aspects however:
Power Query has a more convenient user interface. While Power Queries are relatively
easy to create, the MS Query Wizard is like a website from the 90’s
Power Query stacks operations on top of each other allowing more convenient
changes. While an MS Query works or just doesn’t compile, the Power Query stacks
each transform operation providing visibility into your Data Transformation task, and
making it easier to add / remove operations
In short I encourage learning Power Query if you don’t feel comfortable around SQL. If you are
advanced in SQL I think you will find using good ole Microsoft Queries more convenient. I
would compare this to the Age-Old discussion between Command Line devs vs GUI devs…