【问题标题】:How to tie-out with VBA, Oracle, and Excel using INDEX & MATCH functions?如何使用 INDEX & MATCH 函数与 VBA、Oracle 和 Excel 配合使用?
【发布时间】: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:创建下拉框。管道名称从何而来:硬编码在工作表上还是从数据库中读取?

标签: oracle excel vba indexing


【解决方案1】:

第一季度。看起来您正在尝试进行某种日期匹配,但没有看到实际工作表,很难提供准确的公式。

虽然有一些想法:

  • 你可能已经知道了, 但您需要更改 Cell(i, j).Value 到 Cell(i, j).Formula 和 去掉之前的撇号 "=INDEX" 如果你想让你的公式 实际工作。
  • 在您的 MATCH 函数中,您是 搜索 AH$4 & TEXT($B8,""m/dd/yyyy""。这看起来 对我来说有点可疑。里面有什么 手机AH$4?甲骨文是否吐出一些 日期前的奇怪前缀?如果 你只是在你的 细胞,你不必担心 关于格式(你可以摆脱 的TEXT,只是做一个纯粹的 比较)。 Excel 将日期存储为 “序列号。另外,请查看 DATEVALUE 的帮助。这可能会有所帮助 你把东西转换成 纯枣。
  • 只是一般的建议。得到 您的 MATCH 表达式首先起作用。 然后尝试插入 MATCH 表达式转换为 INDEX 表达式。 这将帮助您隔离您的 确切的问题。

第二季度。看起来您的工作表上已经有管道列表,因此您可以创建一个验证来保证用户只选择合法的管道。

  • 为您的列表指定一个名称 管道。您可以通过选择 您的管道列表,单击 从公式选项卡中定义名称 功能区,然后输入名称。
  • 在工作表上选择一个单元格 用户选择管道。
  • 单击数据选项卡上的数据验证 的功能区。
  • 对于允许,选择列表。
  • 对于源,键入等号 后跟您分配的名称 您的管道列表。所以,如果你的 名称是管道,您将输入 =管道。

现在,当用户单击该单元格时,其中只会出现管道。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2019-09-27
    • 2019-06-26
    相关资源
    最近更新 更多