我在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
答案 0 :(得分: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