循环SQL直到依赖单元格为空

时间:2013-05-06 21:28:04

标签: sql vba while-loop cell

到目前为止,我有这个代码;我试图循环我的SQL搜索,只要单元格I6,第一个带数据的单元格(以及后续单元格)不为空。我无法弄清楚如何使'cusip'依赖于我循环的同一个细胞[第3行...其中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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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