2016-09-19 10 views
0

シートのセル値( "SOURCE")。範囲( "C2")からの日付に基づいてシステムデータを生成するために、次のVBAスクリプトを使用しています。範囲の次の日付を使用するループSQLコード

SQLスクリプトをループし、範囲内の次の日付を使用してコードを繰り返す方法があります(日付は「SOURCE」列Eに記載されています)。ブランクセルに達したらループを終了する必要がありますか?

各データセットは、シート「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

に割り当てることができます。これはあなたがここで持っている質問です。コードを単純化し、連結されたユーザー入力をデータベースに直接送信しないように、パラメーター化されたストアード・プロシージャーをサーバー上で作成することを検討しましたか? –

+0

また、FWIWでは、クエリ結果をワークシートに書き戻すのに最も遅い方法を使用しています。 ['Select'と' ActiveCell'の使用を避けてください(http://stackoverflow.com/q/10714251/1188513)、代わりにオブジェクト参照を扱います。そしてあなたのdbプロバイダにODBCドライバがあるかどうかを確認してください。代わりにADODBレコードセットを使用して、セルごとに書き込むのではなく、ターゲットワークシートに1回の操作で「ダンプ」することができます。 –

答えて

3

あなたは今その今、あなたはSQL

For intRow = 1 To intRowCount 

に値を代入する直前にForループを作成し、列 "E"

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

の行数を得ることができますあなたのループを持って、Sheets("SOURCE").Range("E2")Sheets("SOURCE").Range("E" & intRow)に変更してください。 Do Until dyprod.EOFループでは、値をSheets("DATA").Cells(intRow, 1).Value = ...