I can not seem to get my Excel workbook (using ADODB) to work with Excel Mac 2011. I am using ADODB code. Are there add-ins available? Even from a third-party? Has anyone gotten this to work?
-
@Charles: I rolled back the change you made to my edit. Please note that excel-vba-mac is now a valid tag. – Siddharth Rout Mar 14 '12 at 21:35
-
@SiddharthRout, if it's really a valid tag, then please find other questions to which it can apply. Right now, it applies to this question and this question only, which makes it a *bad* tag. It covers ground already well-covered by existing tags. – Charles Mar 15 '12 at 00:37
-
1@Charles: I would recommend you to please re-read this. "http://stackoverflow.com/privileges/create-tags" I just created that tag and there is no tag which applies to VBA EXCEL MAC. At least I searched for it and couldn't find one. As per the link, give it 6 months. If it is really a bad tag then it will be deleted automatically. ;) – Siddharth Rout Mar 15 '12 at 03:42
-
After review of the tag wikis, there is no need for a new tag for this question, full stop. The [tag:excel] tag covers Excel for both Windows and the Mac and directs the use of the [tag:excel-vba] tag for *all* VBA questions related to Excel. – Charles Mar 15 '12 at 04:36
-
1>>> "After review of the tag wikis, there is no need for a new tag for this question, full stop." I don't want to be impolite but YOU ARE WRONG and if I agree with you then WE BOTH WILL BE WRONG. Excel VBA for MAC is different then Excel VBA for Windows. And probably that is the reason why you have usually different forums for MAC and Windows. I am rolling back the changes as you are being simply stubborn. Please do not force me to flag this to the moderators. – Siddharth Rout Mar 15 '12 at 04:44
-
1FWIW, when creating the original post, I looked for a 'VBA on Mac' tag and, not seeing one, I settled for tagging 'excel' and 'excel-vba' and 'osx' and figured that covered all the bases. So, I guess I see/saw it both ways. :) – n8gard Mar 15 '12 at 15:19
-
1Now you have an exclusive tag for 'VBA on Mac' :) 'osx' is actually a wrong tag for this question. It's something like tagging your Excel questions with "Window 7" or "Vista" :) – Siddharth Rout Mar 15 '12 at 17:19
-
@B5Fan74: Could you please confirm if the download worked for you? – Siddharth Rout Mar 18 '12 at 15:06
3 Answers
ADODB is NOT supported in Mac Excel 2011, but ODBC works in conjunction with a 3rd party driver.
I got my ODBC drivers from ActualTech. Download and install their program and you'll have the necessary drivers for connecting to SQL servers and databases (Free to try, $35 to purchase).
The following code creates a connection to a mySQL database, and returns information from the database into Cell A1:
Dim connstring as String
Dim sqlstring as String
connstring = "ODBC;DRIVER={Actual Open Source Databases};" _
& "SERVER=<server_location>;DATABASE=<database>;" _
& "UID=<userID>;PWD=<password>;Port=3306"
sqlstring = "select * from <database_table>"
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=sqlstring)
.BackgroundQuery = False
.Refresh
End With

- 6,895
- 5
- 45
- 43
-
This is the correct answer; Drivers from OpenLink will also work, but they are simply more expensive. The code worked perfectly and served as my first step; Thanks a lot! – Pbal Jul 26 '17 at 00:07
Are there add-ins available? Even from a third-party?
Hope these download links helps?
ODBC drivers that are compatible with Excel for Mac
Quoted from the MSKB in case the link dies
ODBC drivers that are compatible with Excel for Mac
If you want to import data into Excel for Mac from a database, you need an Open Database Connectivity (ODBC) driver installed on your Mac. The driver you get depends on which version of Excel for Mac you have.
Excel for Mac 2011
This version of Excel does not provide an ODBC driver. You must install it yourself. Drivers that are compatible with Excel for Mac 2011 are available from these companies:
After you install the driver for your source, you can use Microsoft Query to create new queries or refresh existing queries that were created in other versions of Excel, such as Excel X, Excel 2004, and Excel for Windows. For more information, see Import data from a database in Excel for Mac 2011.
Excel 2016 for Mac
This version of Excel does provide an ODBC driver for connecting to SQL Server Databases. On the Data tab, click New Database Query > SQL Server ODBC. Then use the dialog boxes to import the data.
If you are connecting to other ODBC data sources (for example, FileMaker Pro), then you'll need to install the ODBC driver for the data source on your Mac. Drivers that are compatible with Excel for Mac are available from these companies:
Has anyone gotten this to work?
Sorry, I have never used it.

- 147,039
- 17
- 206
- 250
-
1-1, links are not answers. If those links ever go dead or the servers go down, your answer becomes useless. – Charles Mar 14 '12 at 20:26
-
1Please see the meta question [Are answers that just contain links elsewhere really “good answers”?](http://meta.stackexchange.com/q/8231/135887). And yes, Microsoft has downtime. Sometimes *big* downtime, like the leap year Azure glitch – Charles Mar 15 '12 at 00:39
-
Thanks for the links. I am going to try this out and will post my results within the next 24-48 hours. – n8gard Mar 15 '12 at 01:11
-
1No, but you should add *some value* to your answer outside of the links. Explain what they are and give an overview or small quote from the contents that would help someone reading this site understand more *without* having to follow the links. – Charles Mar 15 '12 at 04:37
-
4+1 for the inclination to help others. I don't see why anyone should be discouraged to help in whatever way possible, whether it means posting links or otherwise, especially keeping in view the fact that no one else has even attempted to answer this question. – Pradeep Kumar Mar 15 '12 at 12:44
-
-
_Ever heard of a Microsoft Link Going Down_ it happens all the time - articles get moved, deleted, lost, who knows. Why not replicate the article in the answer? – stuartd Sep 28 '17 at 20:41
-
@stuartd: Sigh.. I agree with you. This post is from 2012 when I had just started posting on SO. I assure you my latest posts are not this shabby :) Will improve this post soon. – Siddharth Rout Sep 29 '17 at 05:01
There are 2 pieces to getting Mac Excel to query MySQL: (1) the VBA and (2) the ODBC driver.
(1) The VBA for creating a table from a query (and updating it with a new query):
Option Explicit
Sub WaitQueryTableRefresh(ByVal qt As QueryTable)
While qt.Refreshing
Application.Wait (Now + TimeValue("0:00:01"))
Wend
End Sub
Sub ErrorIfQueryTableOverflowed(ByVal qt As QueryTable)
If qt.FetchedRowOverflow Then
err.Raise 5, "ErrorIfQueryTableOverflowed", _
"QueryTable '" & qt.ListObject.Name & "' returned more rows than can fit in the spreadsheet range"
End If
End Sub
' Create a table from scratch
Function CreateTableFromSql( _
ByVal table_sheet As Worksheet, _
ByVal table_range As Range, _
ByVal table_name As String, _
ByVal sql As String _
) As ListObject
' table_range is simply the top-left, corner cell for the table
'ListObject.SourceType
'https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.xllistobjectsourcetype?view=excel-pia
'QueryTable.CommandType
'https://learn.microsoft.com/en-us/office/vba/api/Excel.QueryTable.CommandType
'QueryTable.BackgroundQuery
'https://learn.microsoft.com/en-us/office/vba/api/excel.querytable.backgroundquery
'QueryTable.RefreshStyle
'https://learn.microsoft.com/en-us/office/vba/api/excel.xlcellinsertionmode
'QueryTable.PreserveColumnInfo
'https://stackoverflow.com/a/28621172
'https://learn.microsoft.com/en-us/office/vba/api/Excel.QueryTable.PreserveColumnInfo
Dim global_odbc_str As String
global_odbc_str = "ODBC;DSN=my_dsn_name;"
Dim qt As QueryTable
Set qt = table_sheet.ListObjects.Add( _
SourceType:=xlSrcExternal, _
Source:=global_odbc_str, _
Destination:=table_range _
).QueryTable
With qt
.ListObject.Name = table_name
.ListObject.DisplayName = table_name
.CommandText = sql
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.Refresh BackgroundQuery:=False
End With
Call WaitQueryTableRefresh(qt)
Call ErrorIfQueryTableOverflowed(qt)
Set CreateTableFromSql = qt.ListObject
End Function
' Update a table (columns do not have to be the same)
Sub UpdateTableFromSql( _
ByVal table As ListObject, _
ByVal sql As String _
)
Dim qt As QueryTable
Set qt = table.QueryTable
qt.CommandText = sql
qt.Refresh BackgroundQuery:=False
Call WaitQueryTableRefresh(qt)
Call ErrorIfQueryTableOverflowed(qt)
End Sub
(2) Configuring the MySQL ODBC driver (free)
Install according to MySQL docs:
(a) Install (dependency) iODBC Admin: http://www.iodbc.org/dataspace/doc/iodbc/wiki/iodbcWiki/Downloads
(b) Install MySQL ODBC driver: https://dev.mysql.com/downloads/connector/odbc/
(c) Mac requires all apps (including Excel) to be quarantined to a sandbox. Because of this, you need to relocate the MySQL driver to a place where Excel can access it. The symptom of failing to do this is that the DSN connection will Test
successfully in iODBC
but will fail to Test
in Excel's ODBC.
Relocate the driver according to this:
#!/bin/bash
# https://github.com/openlink/iODBC/issues/29
# https://bugs.mysql.com/bug.php?id=89931
base_src_dir="/usr/local"
mysql_odbc_name=$(ls "$base_src_dir" | grep -m 1 "mysql-connector-odbc")
odbc_dir="/Library/ODBC"
src="$base_src_dir/$mysql_odbc_name/lib"
dst="$odbc_dir/$mysql_odbc_name/lib"
echo "creating '$dst'"
sudo mkdir -p "$dst"
echo "copying '$src' to '$dst'"
sudo cp -af "$src/." "$dst"
odbc_ini_path="$odbc_dir/odbc.ini"
odbc_ini_bak_path="$odbc_ini_path.bak"
odbcinst_ini_path="$odbc_dir/odbcinst.ini"
odbcinst_ini_bak_path="$odbcinst_ini_path.bak"
echo "backing up '$odbc_ini_path' to '$odbc_ini_bak_path'"
sudo cp -f "$odbc_ini_path" "$odbc_ini_bak_path"
echo "backing up '$odbcinst_ini_path' to '$odbcinst_ini_bak_path'"
sudo cp -f "$odbcinst_ini_path" "$odbcinst_ini_bak_path"
# https://stackoverflow.com/a/29626460
function replace {
sudo sed -i '' "s/$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<< "$1")/$(sed 's/[&/\]/\\&/g' <<< "$2")/g" "$3"
}
ansi_driver=$(ls "$dst" | grep -m 1 "^lib.*a\.so$")
unicode_driver=$(ls "$dst" | grep -m 1 "^lib.*w\.so$")
old_ansi_path="$src/$ansi_driver"
new_ansi_path="$dst/$ansi_driver"
old_unicode_path="$src/$unicode_driver"
new_unicode_path="$dst/$unicode_driver"
echo "updating '$old_ansi_path' to '$new_ansi_path' in '$odbc_ini_path'"
replace "$old_ansi_path" "$new_ansi_path" "$odbc_ini_path"
echo "updating '$old_ansi_path' to '$new_ansi_path' in '$odbcinst_ini_path'"
replace "$old_ansi_path" "$new_ansi_path" "$odbcinst_ini_path"
echo "updating '$old_unicode_path' to '$new_unicode_path' in '$odbc_ini_path'"
replace "$old_unicode_path" "$new_unicode_path" "$odbc_ini_path"
echo "updating '$old_unicode_path' to '$new_unicode_path' in '$odbcinst_ini_path'"
replace "$old_unicode_path" "$new_unicode_path" "$odbcinst_ini_path"
The above was tested on High Sierra with Excel 2016.

- 3,104
- 1
- 33
- 34