標題:
excel 連接 SQL 2008語法 如何改進 與迴圈
[打印本頁]
作者:
joey0415
時間:
2010-6-4 00:32
標題:
excel 連接 SQL 2008語法 如何改進 與迴圈
小弟利用EXCEL讀取SQL2008的資料,想要從資料庫中抓出來計算,最後想再按照股票代碼跑迴圈,下面是小弟錄製的代碼,我覺得好像可以更簡單一些,請高手指點一下,當然錄製的代碼是2330,最後2330會用成變數來跑,不知道有人可以讓下面的連接方式更好或更快一些!謝謝
小弟只會這樣寫,當然有高手可以幫小弟改的更漂亮一些
Sub 股票資料1()
'
'
Sheets("Sheet1").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DRIVER=SQL Server;SERVER=.;UID=sa;PWD=WXYZ1234;APP=Microsoft Office 2003;WSID=CS5111;DATABASE=stock" _
, Destination:=Range("A1"))
.CommandText = Array( _
"select * from (SELECT TOP 12 * FROM stock.dbo.StockStore WHERE stockid=2330 ORDER BY sdate DESC) AAA ORDER BY sdate")
.Name = "來自 "
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
Sub 股票資料2()
'
'
'
Sheets("Sheet2").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DRIVER=SQL Server;SERVER=.;UID=sa;PWD=WXYZ1234;APP=Microsoft Office 2003;WSID=CS5111;DATABASE=stock" _
, Destination:=Range("A1"))
.CommandText = Array( _
" select * from (SELECT TOP 12 sdate,stockid, CreditMoney,CreditTick FROM stock.dbo.CreditExchangeMonth WHERE stockid=2330 ORDER BY sdate DESC) AAA ORDER BY sdate" _
)
.Name = "來自 "
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
複製代碼
作者:
Hsieh
時間:
2010-6-4 18:31
回復
2#
joey0415
查詢已經存在不再使用Add
假設代號是2030~2032
就同一個查詢使之變化
Sub 股票資料1()
For Each MyID In Array("2030","2031",2032")
With Sheets("Sheet1").QueryTables(1)
.CommandText = Array( _
"select * from (SELECT TOP 12 * FROM stock.dbo.StockStore WHERE stockid=" & MyID & " ORDER BY sdate DESC) AAA ORDER BY sdate")
.Name = "來自 "
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
Next
End Sub
複製代碼
作者:
joey0415
時間:
2010-6-4 21:40
本帖最後由 joey0415 於 2010-6-4 21:44 編輯
[quote]回復 joey0415
查詢已經存在不再使用Add
假設代號是2030~2032
就同一個查詢使之變化
# Sub 股票資料1()
# For Each MyID In Array("2030","2031",2032")
# With Sheets("Sheet1").QueryTables(1)
# .CommandText = Array( _
# "select * from (SELECT TOP 12 * FROM stock.dbo.StockStore WHERE stockid=" & MyID & " ORDER BY sdate DESC) AAA ORDER BY sdate")
# .Name = "來自 "
# .FieldNames = True
# .RowNumbers = False
# .FillAdjacentFormulas = False
# .PreserveFormatting = True
# .RefreshOnFileOpen = False
# .BackgroundQuery = True
# .RefreshStyle = xlInsertDeleteCells
# .SavePassword = True
# .SaveData = True
# .AdjustColumnWidth = True
# .RefreshPeriod = 0
# .PreserveColumnInfo = True
# .Refresh BackgroundQuery:=False
# End With
# Next
複製代碼
請問版主,是不是要先執行輸入帳號與密碼的SQL連線設定,才能使用版主的批次設定,請問要如何增加在版主所寫的程式上呢?感謝版主用心回應
作者:
Hsieh
時間:
2010-6-4 22:12
你既然已經錄製出來
可見你得查詢已經建立
所以帳號密碼應該都被記錄
你先套用看看是否有達到變化
再針對遇到的問題發問
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)