2013-05-06 61 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