我遇到了以下链接:
http://www.informit.com/guides/content.aspx?g=sqlserver&seqNum=135
在其中,他们列出了从 Excel VBA 查询 SQL 数据库的相对简单的代码。
' Declare the QueryTable object
Dim qt As QueryTable
' Set up the SQL Statement
sqlstring = "select au_fname, au_lname from authors"
' Set up the connection string, reference an ODBC connection
' There are several ways to do this
' Leave the name and password blank for NT authentication
connstring = _
"ODBC;DSN=pubs;UID=;PWD=;Database=pubs"
' Now implement the connection, run the query, and add
' the results to the spreadsheet starting at row A1
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=sqlstring)
.Refresh
End With
'Save and close the macro, and run it from the same menu you accessed in step 2.
这工作正常。但是,我希望能够将值作为变量拉回,而不是将其转储到 Excel。
有人可以帮助我吗?我试过寻找 Excel VBA SQL 教程,但我发现的代码似乎有一半不起作用(可能是因为我对它的理解不够好)。