提问人:Kel 提问时间:9/28/2023 最后编辑:BigBenKel 更新时间:10/3/2023 访问量:54
拉取值行/值范围的 SQL 数据
Pulling SQL data for a row/range of values
问:
如果我仅将范围设置为 D5,此代码将对我有用。但是,我想对整行 D 值连续运行它。每当我使该数字超过单个单元格时,它就不再起作用。有什么建议吗?
Dim orderNo As Variant
orderNo = Sheet1.Range("D5:D7").Value
With ActiveWorkbook.Connections("Select BOM").OLEDBConnection
.CommandText = "EXEC dbo.XXX '" & orderNo & "'"
ActiveWorkbook.Connections("Select BOM").Refresh
End With
结束副
答:
0赞
vbakim
10/3/2023
#1
因为您的代码适用于一个单元格值。将 with 部分放入循环内,如下所示。
Dim orderNo as Range
For each orderNo in Sheet1.Range("D5:D7")
With ActiveWorkbook.Connections("Select BOM").OLEDBConnection
.CommandText = "EXEC dbo.XXX '" & orderNo & "'"
ActiveWorkbook.Connections("Select BOM").Refresh
End With
Next orderNo
评论
orderNo
&
Dim cell As Range
, , ?For Each cell in Sheet1.Range("D5:D7")
orderNo = cell.Value