我正在尝试将数据从 SQL 表中提取到 excel 中。我已经使用数据源工具记录了一个宏。但是,我要提取的数据量通常会使 Excel 崩溃。有没有办法在我的 vba 脚本中添加一个变量来限制从 sql 表中提取的数据?本质上是在 sql 中的 select 语句中添加 where 子句。
谢谢!
-肖恩
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=10.22.30.215;Use Procedure for Prepare=1;Aut" _
, _
"o Translate=True;Packet Size=4096;Workstation ID="FakeName";Use Encryption for Data=False;Tag with column collation when possible=Fa" _
, "lse;Initial Catalog=FakeCatelog"), Destination:=Range("$A$1")). _
QueryTable
.CommandType = xlCmdTable
.CommandText = Array( _
"""FakeName""")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceConnectionFile = _
"C:\UFakeFilePathodc"
.ListObject.DisplayName = "FakeName"
.Refresh BackgroundQuery:=False
End With
End Sub`