我使用以下代码来匹配一系列商品编号并返回每个相应的商品编号价格。
虽然返回的价格是正确的,但它不会在正确的行上打印价格。每个价格都打印在应该打印的下面两个单元格。
RowNo =我想要匹配的商品编号,这些编号位于E列。
我错过了什么? :)
SQLStr = "SELECT convert(money, poCOST) FROM [dbo].[Database]" & _
"WHERE [RowNo] IN( '" & Join(Application.Transpose(ActiveSheet.Range("E40:E500").Value), "','") & "')"
Set cn = New ADODB.Connection
With cn
.CursorLocation = adUseClient
.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
Set rs = .Execute(SQLStr)
End With
p = 40
While Not rs.EOF
For v = 1 To rs.Fields.Count
ActiveSheet.Cells(p, v + 10).Value = rs.Fields(v - 1).Value
Next v
p = p + 1
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
cn.Close