0

到目前为止,我有这个代码;我正在尝试循环我的 sql 搜索,只要单元格 I6,包含数据的第一个单元格(以及后续单元格)不为空。我无法弄清楚如何使“cusip”依赖于我正在循环的同一个单元格 [第 3 行 ... where s.cusip = & Sheet1.Range("I6") ]。

即当单元格 I7 不为空时,在查询中使用 I7。

Do While Not IsEmpty(.Cell(I, 6 + C))
    'SQL function'
    oRS.Source = "select s.cusip, s.description, s.rate as coupon, sa.rrb_factor from  security s left join security_analytics sa on s.security_id = sa.security_id where s.cusip = & Sheet1.Range("I6") and sa.as_of_date = trunc(sysdate);"
    oRS.Open  
'copying data into excel'
.cell(W,6+C).CopyFromRecordset oRS

C = C + 1
Loop
4

2 回答 2

1

未经测试,但类似于:

Const COL_CUSIP as long = 9 'I
Const COL_RS as long = 23 'W
C=6     

With Sheet1

Do While Len(.Cells(C, COL_CUSIP).Value)>0
    'SQL function'
    oRS.Source = "select s.cusip, s.description, s.rate as coupon, " & _ 
          " sa.rrb_factor from  security s " & _
          "left join security_analytics sa on " & _ 
          " s.security_id = sa.security_id where s.cusip = " & _
          .Cells(C, COL_CUSIP).Value & " and sa.as_of_date = trunc(sysdate);"
    oRS.Open  
    'copying data into excel'
    if not oRS.EOF then .Cells(C, COL_RS).CopyFromRecordset oRS
    oRS.Close
    C = C + 1
Loop

End With
于 2013-05-06T22:26:29.910 回答
1
Do While Not IsEmpty(.Cell(I, 6 + C))
    'SQL function'
    oRS.Source = "select s.cusip, s.description, s.rate as coupon, sa.rrb_factor " + _
                  "from  security s left join security_analytics sa on " + _
                  "s.security_id = sa.security_id where s.cusip = " + _
                  "Sheet1.Range("I6") + " and sa.as_of_date = trunc(sysdate);"
    oRS.Open  
'copying data into excel'
.cell(W,6+C).CopyFromRecordset oRS

C = C + 1
Loop
于 2013-05-06T22:28:22.230 回答