我正在使用以下VBA脚本根据工作表单元格值的日期生成一些系统数据(" SOURCE")。range(" C2")。
有没有办法循环使用sql脚本并使用范围中的下一个日期重复代码(日期将在表单#34; SOURCE",E列中列出)。当到达空白单元格时,循环应该结束吗?
每组数据应填入表格#34; DATA"中的最后一组结果下方。
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
我确实认为我可以使用以下内容循环遍历日期范围,但我无法弄清楚如何将此链接与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
答案 0 :(得分:3)
您可以获取列&#34; E&#34;
Dim oWS as Worksheet : Set oWS = Worksheets("SOURCE")
Dim intRowCount as Integer : intRowCount = oWS.Cells(Rows.Count, "E").End(xlUp).Row
现在在将值分配给For
SQL
循环
For intRow = 1 To intRowCount
现在您已完成循环,将Sheets("SOURCE").Range("E2")
更改为Sheets("SOURCE").Range("E" & intRow)
。在Do Until dyprod.EOF
循环中,您现在可以将值指定为Sheets("DATA").Cells(intRow, 1).Value = ...