1

Oracle データベースのテーブルに異なる列名があり、VB マクロを使用して Oracle データベースから Excel にデータをフェッチし、異なる列名のデータを異なるシートに並べ替えています。

現在、シートごとに個別のコードを書いていますが、それをループしてコードを簡単にしたい.60のコラボ名があり、すべて異なる名前であり、それらを60の異なるシートにソートしたい

したがって、ループする場合、同じコードを60回書く必要はありません

現在のコード:

    cn.Open ( _
    "User ID=USERID" & _
    ";Password=PASSWORD" & _
    ";Data Source=xx.xx.xx.xxx:xxxx/xxxx" & _
    ";Provider=OraOLEDB.Oracle")


    rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like 'COLLAB_NAME1' ORDER BY DATETIME ASC", cn
    With Sheet1
            col = 0
             'First Row: names of columns
            Do While col < rs.Fields.Count
                .Cells(1, col + 1) = rs.Fields(col).Name
                col = col + 1
            Loop


            mtxData = Application.Transpose(rs.GetRows)
            .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData




        End With
        rs.Close

  rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like 'COLLAB_NAME_2' ORDER BY DATETIME ASC", cn
    With Sheet2
            col = 0
             'First Row: names of columns
            Do While col < rs.Fields.Count
                .Cells(1, col + 1) = rs.Fields(col).Name
                col = col + 1
            Loop


            mtxData = Application.Transpose(rs.GetRows)
            .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData




        End With
        rs.Close

 rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like 'COLLAB_NAME1_NAME2_3' ORDER BY DATETIME ASC", cn
    With Sheet3
            col = 0
             'First Row: names of columns
            Do While col < rs.Fields.Count
                .Cells(1, col + 1) = rs.Fields(col).Name
                col = col + 1
            Loop


            mtxData = Application.Transpose(rs.GetRows)
            .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData




        End With
        rs.Close
    End Sub

新しいコード:

Sub Load_data()
        Sheets("Sheet1").Select
        Dim cn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim col As Integer
        Dim row As Integer
        Dim Query As String
        Dim mtxData As Variant


        Set cn = New ADODB.Connection
        Set rs = New ADODB.Recordset

     cn.Open ( _
    "User ID=USERID" & _
    ";Password=PASSWORD" & _
    ";Data Source=xx.xx.xx.xxx:xxxx/xxxxxx" & _
    ";Provider=OraOLEDB.Oracle")

       'Creates an Array with all the Collabnames.

Dim arrayCOLLABNAME(59) As String
Dim idx As Integer
idx = 0
Sheets("COLLABNAME").Select
Range("A1").Select
Do While Not ActiveCell.Offset((idx), 0) = ""
    arrayCOLLABNAME(idx) = ActiveCell.Offset(idx, 0).Value
    idx = idx + 1
Loop
'The name of the First Sheet
    Sheets("Sheet1").Select
'Loop for 60 Sheets
For i = 0 To 60
    'adds the new Sheet
    Sheets.Add
    rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like '" & arrayCOLLABNAME(idx) & "' ORDER BY DATETIME ASC", cn
    'use the new Sheet for inserting the Data
    With ActiveSheet
        col = 0
        'First Row: names of columns
        Do While col < rs.Fields.Count
            .Cells(1, col + 1) = rs.Fields(col).Name
            col = col + 1
        Loop
        mtxData = Application.Transpose(rs.GetRows)
        .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData

    End With
    rs.Close
Next i


    End Sub
4

1 に答える 1

2

私はそれがあなたのために働くことができると思います. しかし、私はそれをテストする時間がありませんでした。

* *新しいコード

    'Creates an Array with all the Collabnames.
Dim arrayCOLLABNAME(59) As String
Dim idx As Integer
idx = 0
Sheets("COLLABNAME").Select
Range("A1").Select
Do While Not ActiveCell.Offset((idx), 0) = ""
    arrayCOLLABNAME(idx) = ActiveCell.Offset(idx, 0).Value
    idx = idx + 1
Loop

'The name of the First Sheet
    Sheets("Tabelle1").Select
'Loop for 60 Sheets
For i = 0 To 60
    'adds the new Sheet
    Sheets.Add
    rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like '" & arrayCOLLABNAME(idx) & "' ORDER BY DATETIME ASC", cn
    'use the new Sheet for inserting the Data
    With ActiveSheet
        col = 0
        'First Row: names of columns
        Do While col < rs.Fields.Count
            .Cells(1, col + 1) = rs.Fields(col).Name
            col = col + 1
        Loop
        mtxData = Application.Transpose(rs.GetRows)
        .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData

    End With
    rs.Close
Next i
于 2012-04-16T09:26:06.663 に答える