【发布时间】:2009-07-13 13:50:44
【问题描述】:
我正在尝试使用 VBA 创建一个程序,该程序可针对电子表格查询我的 oracle 数据库数据(在本例中为管道),并在同一工作簿(但在另一张工作表上)生成绑定输出。我想在 tie-out 页面上使用 INDEX 和 MATCH 函数,但无法弄清楚。这是我到目前为止所拥有的(下面的图形表示),这是我的问题:
我有什么:
我的工作簿上有 4 个标签:
1. 电子表格数据
2. 说明(提示用户输入管道、查询的结束/开始日期)
3. 数据库输出数据(VBA脚本会从Oracle数据库拉取数据)
4. 绑定数据选项卡(这是我遇到问题的地方)
问题:
1) 如何使用 INDEX 和 MATCH 从我的 oracle 数据库表和电子表格中查找值?
2)我如何创建一个下拉框以便用户选择一个管道,而不是提示用户使用管道?这将减少任何用户输入错误(例如拼写等)。
谢谢!
我的代码:
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:创建下拉框。管道名称从何而来:硬编码在工作表上还是从数据库中读取?