0
我有點卡住了一段時間在一個小項目 生成結果從幾個 SQL查詢在幾個Excel表格,我想使用SQL Server 2008,這是我第一次編碼VBA 我想這個代碼(對於SQL單個查詢),但我仍然有編譯問題執行SQL查詢從excel
Sub New_Feuil1()
ThisWorkbook.Activate
'First clear the contents from the query
Worksheets("Feuil1").Select
Range("A2").Select
Do Until ActiveCell = ""
ActiveCell.Offset(1).Select
Loop
Range("A4", ActiveCell.Offset(-1, 3)).ClearContents
'Get reporting date
ReportingDate = ThisWorkbook.Sheets("Parameters").Range("D1")
'Format the value for use in the SQL query
ReportingDateFor = Format(ReportingDate, "yyyy-mm-dd")
Worksheets("Feuil1").Select
Range("A1").Select
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim StrQuery1 As String
Dim ConnectionString As String
ConnectionString ="ODBC;" & _
"Driver={SQL Server Native Client 10.0};" & _
"Server=187.125.254.231;" & _
"Database=database;" & _
"UID=sa; PWD=pwd"
cnn.Open ConnectionString
cnn.CommandTimeout = 900
'Queries to be executed
StrQuery1 = StrQuery1 & "Select Id from Users"
rst.Open StrQuery1, cnn, adOpenForwardOnly, adLockReadOnly
rst.Close
Debug.Print "StrQuery1:"; StrQuery1
cnn.Close
ThisWorkbook.Sheets("Central Dashboard").Select
Sheets("Feuil1").Range("A2").CopyFromRecordset rst
End Sub
還有沒有其他的解決辦法?
能否請你告訴我們具體的** **的問題(S)你有。閱讀[如何編寫一個簡短的,自包含的,正確的示例](http://sscce.org/)並查看此處的[help center](http://stackoverflow.com/help)。然後[編輯](http://stackoverflow.com/posts/25524210/edit)你的問題,並提供所有必要的細節。 – RossC 2014-08-27 10:06:28
每當我運行宏時,它會一直告訴我,我有一個compulation問題 – user3708197 2014-08-27 10:11:16
好的...什麼是編譯問題?我看不到您的電腦。請閱讀我提供的鏈接並編輯您的問題,向我們解釋發生了什麼事情。 「它給出了一個編譯器錯誤」告訴我什麼都沒有。 – RossC 2014-08-27 10:13:08