使用范围中的下一个date循环sql代码

我正在使用以下VBA脚本根据工作表单元格值(“SOURCE”)。range(“C2”)中的date生成一些系统数据。

有没有办法循环sql脚本,并重复使用范围中的下一个date的代码(date将列在表“源”,列E)。 到达空白单元格时,循环应该结束?

每组数据应填充在“数据”表中的最后一组结果之下。

Sub DATA() Sheets("DATA").Select Cells.Select Selection.Delete Shift:=xlUp Range("A1").Select Dim SQL As String Dim orasession As Object Dim oradatabase As Object Dim dyprod As Object Dim Row As Long Application.ScreenUpdating = False Set orasession = CreateObject("OracleInProcServer.XOraSession") 'Create the OraSession Object. ( Oracle ) Set oradatabase = orasession.DbOpenDatabase("thal_cnded.world", "bde_rep/report", 0&) 'Create the OraDatabase Object by opening a connection to Oracle. SQL = SQL & "select (select min(trunc(cb.act_date)) from com_bde_ahp_log cb " SQL = SQL & "where (cb.prod_mach like 'M%' or cb.prod_mach like 'B%') and m.wcenter = cb.wcenter (+) and cb.prod_plant = 'W' and cb.diff_ok_disc_qty > 0 " SQL = SQL & "and cb.act_date >= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 06:10:00 ', 'DD/MM/YYYY HH24:MI:SS') " SQL = SQL & "and cb.act_date <= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 18:09:59', 'DD/MM/YYYY HH24:MI:SS')) as one, " SQL = SQL & "m.machine as two, " SQL = SQL & "(select nvl(sum(cb.diff_ok_disc_qty),0) from com_bde_ahp_log cb " SQL = SQL & "where m.machine = cb.prod_mach And m.wcenter = cb.wcenter And m.prod_plant = cb.prod_plant And cb.diff_ok_disc_qty > 0 " SQL = SQL & "and cb.act_date >= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 06:10:00', 'DD/MM/YYYY HH24:MI:SS') " SQL = SQL & "and cb.act_date <= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 18:09:59', 'DD/MM/YYYY HH24:MI:SS')) as three, " SQL = SQL & "(select nvl(sum(c.change_m),0) " SQL = SQL & "from (select distinct mach, count((previous_order)) change_m " SQL = SQL & "from (select c.prod_mach mach, c.act_date, c.part_no, c.wcenter wcenter, m.machgrp machgrp, " SQL = SQL & " NVL((select distinct c1.part_no from com_bde_ahp_log c1 " SQL = SQL & " where c1.wcenter not like 'D%' and c1.prod_plant = 'W' and c1.ast = 51 and c1.prod_mach||'-'||c1.cavity = c.prod_mach||'-'||c.cavity and rownum = 1 " SQL = SQL & " and c1.act_date = " SQL = SQL & " (select max(c2.act_date) from com_bde_ahp_log c2 " SQL = SQL & " where c2.wcenter not like 'D%' and c2.prod_plant = 'W' and c2.ast = 51 and c2.prod_mach||'-'||c2.cavity = c.prod_mach||'-'||c.cavity " SQL = SQL & " and c2.act_date < c.act_date and c2.act_date between c.act_date-0.5 and c.act_date)),'NA') previous_order, p.grpname format " SQL = SQL & "from machine_master_data m, com_bde_ahp_log c " SQL = SQL & "left join (select grpname,prodtyp, plant, packtyp from RLS_PROD_GROUP where grpname in ('BD25','BD50','DVD_5','DVD_9','DVD_10','UMD_2','UMD_1')) p on p.prodtyp = c.prodtyp and c.prod_plant = p.plant and substr(c.packtyp,2,1) = substr(p.packtyp,2,1) " SQL = SQL & "where c.wcenter not like 'D%' " SQL = SQL & "and c.act_date >= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 06:10:00', 'DD/MM/YYYY HH24:MI:SS') " SQL = SQL & "and c.act_date <= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 18:09:59', 'DD/MM/YYYY HH24:MI:SS') " SQL = SQL & "and to_char(c.act_date,'hh24:mi:ss') between (select substr(t.time,2) from bde_report_times_v t where plant = 'W' and seq_nr = 1) and (select substr(t.time,2) from bde_report_times_v t where plant = 'W' and seq_nr = 2) " SQL = SQL & "and c.prod_plant = 'W' and c.ast = 51 and m.machine = c.prod_mach and m.wcenter = c.wcenter and m.prod_plant = c.prod_plant " SQL = SQL & "group by c.prod_mach, c.cavity, c.part_no, c.wcenter, c.act_date, m.machgrp, c.prod_mach, p.grpname order by 1,2) where previous_order != part_no " SQL = SQL & "group by mach, wcenter, machgrp, format order by 1) c where c.mach = m.machine) as four, " SQL = SQL & "(select nvl(sum(cb.diff_ok_disc_qty),0) from com_bde_ahp_log cb " SQL = SQL & "where m.machine = cb.prod_mach And m.wcenter = cb.wcenter And m.prod_plant = cb.prod_plant And cb.diff_ok_disc_qty > 0 " SQL = SQL & "and cb.act_date >= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 18:10:00', 'DD/MM/YYYY HH24:MI:SS') " SQL = SQL & "and cb.act_date <= (to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 06:09:59', 'DD/MM/YYYY HH24:MI:SS'))+1) as five, " SQL = SQL & "(select nvl(sum(c.change_m),0) from (select distinct mach, count((previous_order)) change_m " SQL = SQL & "from (select c.prod_mach mach, c.act_date, c.part_no, c.wcenter wcenter, m.machgrp machgrp, " SQL = SQL & " NVL((select distinct c1.part_no from com_bde_ahp_log c1 " SQL = SQL & " where c1.wcenter not like 'D%' and c1.prod_plant = 'W' and c1.ast = 51 and c1.prod_mach||'-'||c1.cavity = c.prod_mach||'-'||c.cavity and rownum = 1 " SQL = SQL & " and c1.act_date = (select max(c2.act_date) " SQL = SQL & " from com_bde_ahp_log c2 " SQL = SQL & " where c2.wcenter not like 'D%' and c2.prod_plant = 'W' and c2.ast = 51 and c2.prod_mach||'-'||c2.cavity = c.prod_mach||'-'||c.cavity " SQL = SQL & " and c2.act_date < c.act_date and c2.act_date between c.act_date-0.5 and c.act_date)),'NA') previous_order, p.grpname format " SQL = SQL & "from machine_master_data m, com_bde_ahp_log c " SQL = SQL & "left join (select grpname, prodtyp, plant, packtyp from RLS_PROD_GROUP where grpname in ('BD25','BD50','DVD_5','DVD_9','DVD_10','UMD_2','UMD_1')) p on p.prodtyp = c.prodtyp and c.prod_plant = p.plant and substr(c.packtyp,2,1) = substr(p.packtyp,2,1) " SQL = SQL & "where c.wcenter not like 'D%' " SQL = SQL & "and c.act_date >= to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 18:10:00', 'DD/MM/YYYY HH24:MI:SS') " SQL = SQL & "and c.act_date <= (to_date(' " & Sheets("SOURCE").Range("E2") & " '||' 06:09:59', 'DD/MM/YYYY HH24:MI:SS'))+1 " SQL = SQL & "and ((to_char(c.act_date,'hh24:mi:ss') between (select substr(t.time,2) from bde_report_times_v t where plant = 'W' and seq_nr = 5) and '23:59:59') or (to_char(c.act_date,'hh24:mi:ss') between '00:00:00' and (select substr(t.time,2) from bde_report_times_v t where plant = 'W' and seq_nr = 6))) " SQL = SQL & "and c.prod_plant = 'W' and c.ast = 51 and m.machine = c.prod_mach and m.wcenter = c.wcenter and m.prod_plant = c.prod_plant " SQL = SQL & "group by c.prod_mach, c.cavity, c.part_no, c.wcenter, c.act_date, m.machgrp, c.prod_mach, p.grpname order by 1,2) where previous_order != part_no " SQL = SQL & "group by mach, wcenter, machgrp, format order by 1) c where c.mach = m.machine) as six " SQL = SQL & "from machine_master_data m " SQL = SQL & "where (m.machine like 'M%' or m.machine like 'B%') " SQL = SQL & "and m.prod_plant = 'W' " SQL = SQL & "order by length(m.machine), m.machine " Set dyprod = oradatabase.CreateDynaset(SQL, 0&) Sheets("DATA").Select Row = 2 If Not dyprod.EOF And Not dyprod.bof Then dyprod.movefirst Do Until dyprod.EOF Sheets("DATA").Cells(Row, 1).Select ActiveCell.Value = dyprod.Fields("one").Value Sheets("DATA").Cells(Row, 2).Select ActiveCell.Value = dyprod.Fields("two").Value Sheets("DATA").Cells(Row, 3).Select ActiveCell.Value = dyprod.Fields("three").Value Sheets("DATA").Cells(Row, 4).Select ActiveCell.Value = dyprod.Fields("four").Value Sheets("DATA").Cells(Row, 5).Select ActiveCell.Value = dyprod.Fields("five").Value Sheets("DATA").Cells(Row, 6).Select ActiveCell.Value = dyprod.Fields("six").Value dyprod.movenext Row = Row + 1 Loop End If End Sub 

我确实认为我可以使用以下来循环访问date范围,但是我无法弄清楚如何链接这个SQL和使数据出现在最后一组结果下?

 Sub DateLoop() Dim x As Integer NumRows = Range("E2", Range("E2").End(xlDown)).Rows.Count Range("E2").Select For x = 1 To NumRows ActiveCell.Offset(1, 0).Select Next End Sub 

你可以得到“E”列的行数

 Dim oWS as Worksheet : Set oWS = Worksheets("SOURCE") Dim intRowCount as Integer : intRowCount = oWS.Cells(Rows.Count, "E").End(xlUp).Row 

现在在将值赋给SQL之前创build一个For循环

 For intRow = 1 To intRowCount 

现在你有了你的循环,把你的Sheets("SOURCE").Range("E2")改为Sheets("SOURCE").Range("E" & intRow) 。 在您的Do Until dyprod.EOF循环中,您现在可以将值分配为Sheets("DATA").Cells(intRow, 1).Value = ...