I have a report where I run the same MDX query each week, and paste the results in a worksheet to then generate a report. I know how to connect to the OLAP cube in excel, but I don't like manipulating the pivot table to match my query.
I'd like to use VBA to connect to the cube and and return the results of the specific query in a worksheet as a values paste, or table. I can save the MDX query in a seperate sheet. Would this be possible? I'm new to VBA, so I'm not sure where to get started. Any example code would be great.
答案 0 :(得分:1)
我认为前一个问题非常符合您的需求:
Any MDX query within Excel vba?
我调整了代码,因为之前的问题 - 答案只是写了下面的内容似乎返回了一个数字确定:
Sub getFromCube()
Dim strConn As String
strConn = _
"Provider=MSOLAP.6;" & _
"Data Source=imxxxxxx;" & _ '<<<name of your server here
"Initial Catalog=AdventureWorksDW2012Multidimensional-EE;" & _ '<<<name of your Adv Wrks db here
"Integrated Security=SSPI"
Dim pubConn As ADODB.Connection
Set pubConn = New ADODB.Connection
pubConn.CommandTimeout = 0
pubConn.Open strConn
Dim cs As ADOMD.Cellset
Set cs = New ADOMD.Cellset
Dim myMdx As String
myMdx = _
" SELECT" & _
" NON EMPTY" & _
" [Customer].[Customer Geography].[State-Province].&[AB]&[CA] ON 0," & _
" NON EMPTY" & _
" [Measures].[Internet Sales Amount] ON 1" & _
" FROM [Adventure Works];"
With cs
.Open myMdx, pubConn
ActiveSheet.Range("A1") = cs(0, 0)
.Close
End With
End Sub
如果您查看上一个问题,您会发现获取数据的单元格非常容易,但是将其粘贴到工作表中并不是那么简单。为了简单起见并快速检查事情是否按预期工作,我只使用了这个ActiveSheet.Range("A1") = cs(0, 0)
。我想你需要循环遍历单元格。
注意 - 您需要为上述内容添加两个引用:
(或您可用的每个版本的最新版本)