爲什麼不使用DATAEXTRACTION的SSIS包或SSRS報告嗎?宏是如此......上個世紀。
如果你堅持使用宏,這裏有一個例子:
Dim con As ADODB.Connection, _
cmd As ADODB.Command, _
rs As ADODB.Recordset, _
strSQL As String, _
i As Integer
strSQL = "EXEC SP_StoredProcedureToGetData"
Set con = New ADODB.Connection
con.Open "Provider=SQLOLEDB;Data Source=SQLSERVER;" & _
"Initial Catalog=DATABASE;User ID=USERNAME;Password=PASSWORD"
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = con
.CommandText = strSQL
.CommandType = adCmdStoredProc
End With
Set rs = New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = adLockBatchOptimistic
End With
Set rs = cmd.Execute
Cells(1, 1) = "Column1"
Cells(1, 2) = "Column2"
Cells(1, 3) = "Column3"
Cells(1, 4) = "Column4"
Cells(1, 5) = "Column5"
Cells(2, 1).CopyFromRecordset rs
Set con = Nothing
Set cmd = Nothing
Set rs = Nothing
你是說所有的SQL行被壓縮成一個單一的線?或者每行都在一個列中? – LittleBobbyTables 2012-07-25 15:14:58
@LittleBobbyTables,對不起,我的意思是說每一行都是一列。 – 2012-07-25 15:28:14
事實之後,您應該可以只是將'Data> Text to Columns'。 – LittleBobbyTables 2012-07-25 15:29:56