excel 拉取一行/一系列值的SQL数据

b1payxdu  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(86)

如果我只为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

End Sub

cbeh67ev

cbeh67ev1#

因为你的代码只处理一个单元格值。将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

相关问题