Sql 在 Mac 中查询 Excel sheet

Sql query for an Excel sheet in Mac

我在 Excel 2016 年通过 oledb 连接在 windows 电脑上写了一个宏。但它在我的 Mac 中不起作用。我怎么能运行这个?通常,在 windows 中,您选择工具 > 参考并检查 oledb 连接,但在 Excel Mac 中没有工具选项。我不知道代码在这里很重要,但我还是会分享它。

Dim sorgu1 As String
Dim con As Object, rs As Object
    Set con = CreateObject("adodb.connection")
    Set rs = CreateObject("adodb.recordset")
    con.Open "provider=microsoft.ace.oledb.12.0;data source=" & _
    ThisWorkbook.FullName & ";extended properties=""Excel 12.0;hdr=yes"""

    makine = Sheets("Dashboard").Cells(2, 11).Value

    sorgu1 = "select [Resource Id], [Order No], Sum(([Bitim Zamani]-[Basl Zamani])*1440)" 'as Plansiz, Sum(([Bitim Zamani]-[Basl Zamani])*1440)as Planli, Sum([Qty]) as Uretim"
    sorgu1 = sorgu1 + " from [Data$] where [Resource Id] = " + "'" + makine + "'"
    sorgu1 = sorgu1 + "GROUP BY [Resource Id], [Order No], [Duru Kodu]"

    Set rs = con.Execute(sorgu1)
    Sheets("Dashboard").Range("B65536").End(3).Offset(1, 0).CopyFromRecordset rs
    Set rs = Nothing

您的代码使用后期绑定并且不需要“参考”,只要该库在机器上可用即可。在您 Mac OS 的情况下,该库不存在,据我所知,明确不允许。

From Microsoft(强调已加):

To import data from a database, such as Microsoft SQL Server, you must have an ODBC driver that is compatible with Microsoft Query installed on your computer. Compatible ODBC drivers are available from third-party vendors. For more information, see ODBC drivers that are compatible with Excel for Mac. For more information about installing ODBC drivers, see Microsoft Query Help.

重要的是,对于您的问题,如果对 Mac 使用 Excel 2011。Mac OS 明确不支持 OLE DB。

Excel 2011 for Mac cannot import data from OLE DB or OLAP data sources.

因此,要回答您的问题,无法使 您的 SQL 查询在 Mac 上运行,您需要完全使用其他东西.

但是,如前所述,如果您将 Excel 2016 用于 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:

或者,类似问题的这个答案看起来很有希望: