【问题标题】:Any MDX query within Excel vba?Excel vba中的任何MDX查询?
【发布时间】:2012-06-25 11:44:26
【问题描述】:

有没有办法在 Excel VBA 中执行 MDX 查询?

我认为它可以通过ADO 完成,类似于在 SQL 案例中(是的,我知道 SQL 与 MDX 不同 - 在 Stackoverflow 上多次提到的问题)。
很遗憾,我找不到任何示例。

  • 有人说要使用外部工具来完成这项任务,但我 不想为他们付钱。
  • 有些人在 XMLA 中给出了示例,但我想执行简单的 MDX 查询 而是

.

【问题讨论】:

标签: vba mdx


【解决方案1】:

我们有以下在 VBA 中调用的通用函数,它基于输入的 MDX 字符串将数据写入 Excel。电子表格确实需要引用 ADO 和 ADOMD

Public Sub DisplayMDX(ipCell, ipMDX, ipExclHeadings)

    Dim sQry As String
    Dim sConnection As String
    Dim rs As ADOMD.Cellset
    Dim sServer, sDB, ts As String
    Dim hyper As Hyperlink
    Dim i, j, k, h, rowStart, colStart, dimCount As Integer
    Dim sURLLink, sCustCaption, sCustLink As String
    Dim db As ADODB.Connection

    'Open a new ADO connection
    Set db = New ADODB.Connection
    sConnection = "Provider=MSOLAP; Data Source=DW3; Initial Catalog=FDMDW1; Integrated Security=SSPI"

    db.CommandTimeout = 0
    db.Open sConnection

    'Open a CellSet to store the results of the query.
    Set rs = New Cellset

    'Tidy the query of an erroneous spaces
    sQry = Trim(ipMDX)

    'Open the query that was constructed above
    Application.StatusBar = "Getting OLAP Data"
    With rs
        .Open sQry, db
    End With

    With ActiveSheet

     'Goto cell specified
     Range(ipCell).Select

     'Find the starting point
     rowStart = ActiveCell.Row
     colStart = ActiveCell.Column
     For j = 0 To rs.Axes(1).Positions.Count - 1

        If Not ipExclHeadings Then
           dimCount = rs.Axes(1).DimensionCount
           For h = 0 To rs.Axes(1).DimensionCount - 1
                Cells(rowStart + j, colStart + h) = rs.Axes(1).Positions(j).Members(h).Caption
           Next
        End If

        For k = 0 To rs.Axes(0).Positions.Count - 1
           If Not (k = 1) Then

              If rs(k, j) <> "" Then
                 Cells(rowStart + j, colStart + dimCount + k).Value = rs(k, j)
              Else
                 Cells(rowStart + j, colStart + dimCount + k).ClearContents
              End If

           End If
           Application.StatusBar = rs(k, j)
        Next

     Next
    End With

rs.Close

Application.StatusBar = "Done"

Exit Sub
errMsg:
   MsgBox Err.Description, vbOKOnly + vbCritical, "Error #" & Err.Number

End Sub

【讨论】:

  • 我找不到您在代码中使用“ipLink”的位置。请描述一下我们为什么需要它?
  • 此处代码崩溃:Dim rs As ADOMD.Cellset(类型未定义),所以我在工具中添加了 Microsoft ActiveX Data Objects (Multi-dimensional) 2.7 Library Microsoft ActiveX Data Objects 2.7 Library
  • 用于分析解析器的 XML:输入查询不是此请求的方言属性中指定的语言。错误代码-2147467259
  • 我的 MDX 是“SELECT CUBE_NAME FROM $SYSTEM.MDSchema_Cubes where cube_source=1”。它在 Analysis Services 中调用时起作用。
  • 对不起,我应该把 ipLink 去掉。是的,你可以删除它。我用它在电子表格中嵌入了一个链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2017-06-06
  • 1970-01-01
  • 2011-09-16
  • 2014-10-03
  • 1970-01-01
相关资源
最近更新 更多