我正在嘗試使用VBA創建一個程序,該程序根據電子表格查詢我的oracle數據庫數據(在本例中爲管道),並在同一工作簿(但在另一個工作表上)生成綁定輸出。我想使用綁定頁面上的INDEX和MATCH函數,但無法解決這個問題。以下是我迄今爲止(以下圖示),這裏是我的問題:如何使用INDEX和MATCH函數與VBA,Oracle和Excel結合?
我有:
我有我的工作簿4個選項卡:
1.電子表格數據
2.說明(提示用戶輸入管道,查詢結束/開始日期)
3.數據庫輸出數據(VBA腳本將從Oracle數據庫中提取數據)
4.列出數據選項卡(這是我遇到的問題)
問題:
1)如何使用INDEX和MATCH查找Oracle數據庫表和我的電子表格中的查找值?
2)我可以如何創建一個下拉框,以便用戶選擇一個管道,而不是將管道提供給用戶?這將減少任何用戶輸入錯誤(如拼寫等)。
謝謝!
alt text http://i27.tinypic.com/260plvn.jpg
alt text http://i26.tinypic.com/1zznqrt.jpg
我的代碼:
Option Explicit
Option Base 1
'user is prompted for dates and pipeline name
'click button will prompt query
Dim cnnObject As ADODB.Connection
Dim rsObject As ADODB.Recordset
Dim strGPOTSConnectionString As String
'this will remove old sql data upon new query
Dim ws As Worksheet
Dim Pipeline As String
Dim DateStart As Date
Dim DateEnd As Date
Dim strQuery As String
Sub Say(s As String)
Debug.Print s
End Sub
Sub ClickButton2()
Debug.Print ("Button has been clicked")
'KGK
Set ws = Worksheets("ZaiNet Data")
ws.UsedRange.Clear '' remove results of previous query if any
'this will fill in null values in query as "data not available"
Pipeline = InputBox("Enter PipeLine", "My Application", "Default Value")
DateStart = InputBox("Enter Start Date", "My Application", DateTime.Date)
DateEnd = InputBox("Enter End Date", "My Application", DateTime.Date + 1)
Range("B1").Value = Pipeline
Range("B2").Value = DateStart
Range("B3").Value = DateEnd
'KGK: call to function to populate the IN() part of the SQL statement
Dim dtInDate As String
dtInDate = GetIN(DateStart, DateEnd)
Debug.Print (" ")
'strQuery = "select pipelineflow.lciid lciid, ldate, volume, capacity, status, " & _
"pipeline, station, stationname, drn, state, county, owneroperator, companycode, " & _
"pointcode, pointtypeind, flowdirection, pointname, facilitytype, pointlocator, " & _
"pidgridcode from pipelineflow, pipelineproperties " & _
"where pipelineflow.lciid = pipelineproperties.lciid " & _
"and pipelineflow.audit_active = 1 " & _
"and pipelineproperties.audit_active =1 " & _
"and pipelineflow.ldate >= '" & Format(DateStart, "m/d/yyyy") & "' and pipelineflow.ldate < '" & Format(DateEnd, "dd-MMM-yyyy") & "' " & _
"and pipelineproperties.pipeline = '" & Pipeline & "' "
strQuery = "select pipelineflow.lciid lciid, ldate, volume, capacity, status, " & _
"pipeline, station, stationname, drn, state, county, owneroperator, companycode, " & _
"pointcode, pointtypeind, flowdirection, pointname, facilitytype, pointlocator, " & _
"pidgridcode from pipelineflow, pipelineproperties " & _
"where pipelineflow.lciid = pipelineproperties.lciid " & _
"and pipelineflow.audit_active = 1 " & _
"and pipelineproperties.audit_active =1 " & _
"and pipelineflow.ldate " & dtInDate & _
"and pipelineproperties.pipeline = '" & Pipeline & "' "
'KGK: modify strQuery
'Debug.Print (strQuery)
Call PullZaiNetData(strQuery)
Call TieOut
End Sub
Sub PullZaiNetData2(ByVal strQry As String)
Set cnnObject = New ADODB.Connection
Set rsObject = New ADODB.Recordset
strGPOTSConnectionString = "DRIVER={Microsoft ODBC for Oracle}; SERVER=XXX; PWD=XXX; UID=XXX"
cnnObject.Open strGPOTSConnectionString
'this will give a record count and will help to verify values
'rsObject.Open strQry, cnnObject, adOpenStatic
'Say rsObject.RecordCount & " records"
'If rsObject.RecordCount = 0 Then
' ws.Cells(1, 1) = "DATA NOT AVAILABLE"
'Else
' ws.Cells(1, 1).CopyFromRecordset rsObject
'End If
rsObject.Open strQry, cnnObject, adOpenStatic
Worksheets("ZaiNet Data").Cells(1, 1).CopyFromRecordset rsObject
rsObject.Close
cnnObject.Close
Set rsObject = Nothing
Set cnnObject = Nothing
End Sub
Sub TieOut()
Dim i As Integer
Dim j As Integer
For i = 1 To 3
For j = 1 To 3
Worksheets("TieOut").Cells(i, j).Value = "'=INDEX('database data for all dates '!$A$1:$U$314,MATCH(AH$4&TEXT($B8,""m/dd/yyyy""),'database data for all dates '!$C$1:$C$314,0),4)"
Next j
Next i
End Sub
Public Function GetIN(ByVal startDate As Date, ByVal endDate As Date) As String
Dim arrDates() As Date
Dim currentDate As Variant
Dim dateInterval As Integer
Dim strIN As String
Dim i As Integer
dateInterval = DateDiff("d", startDate, endDate)
dateInterval = dateInterval + 1
ReDim arrDates(1 To dateInterval)
For i = 1 To dateInterval
arrDates(i) = DateAdd("d", i - 1, startDate)
Next i
'debug loop
'For i = 1 To dateInterval
' Debug.Print ("i: " & i & ", date: " & arrDates(i))
'Next i
'Convert the date array to string
strIN = " IN("
For i = 1 To UBound(arrDates)
strIN = strIN & "'" & CStr(Format(arrDates(i), "m/d/yyyy")) & "'"
If i < UBound(arrDates) Then
strIN = strIN & ", "
End If
Next
strIN = strIN & ") "
'debug statement
'Debug.Print (strIN)
GetIN = strIN
End Function
re:創建下拉框。管道名稱來自哪裏:在工作表上硬編碼還是從數據庫中讀取? – barrowc 2009-07-13 23:10:45