2013-05-06 24 views
0

到目前为止,我有这个代码;我试图循环我的sql搜索,只要单元格I6,数据的第一个单元格(以及后续单元格)不是空的。我无法弄清楚如何使'cusip'取决于我正在循环的同一个单元[第3行...其中s.cusip = & Sheet1.Range(“I6”)]。循环SQL直到相关单元格为空

即当单元格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 
+0

为什么你不尝试使用MS查询? – user2140261 2013-05-06 22:23:18

回答

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
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