[英]VBScript Calling VBA Macro to build Pivot Table
I am trying to do the following:我正在尝试执行以下操作:
I have successfully exported the data (step 1) and triggered an Excel Macro (Step 2).我已成功导出数据(步骤 1)并触发了 Excel 宏(步骤 2)。
This macro should theoretically achieve Step 3 (if I run it manually from Excel, it completes successfully).这个宏理论上应该完成第 3 步(如果我从 Excel 手动运行它,它会成功完成)。
However, when the Macro that is triggered in Step 3 creates the Pivot Table, it then fails and in QlikView, the VBScript message is:但是,当在步骤 3 中触发的宏创建数据透视表时,它会失败,并且在 QlikView 中,VBScript 消息是:
Cannot run the macro 'procBuildPivot'.无法运行宏“procBuildPivot”。 The macro may not be available in this workbook or all macros may be disabled.宏在此工作簿中可能不可用,或者可能禁用了所有宏。
This is definitely not the case as, if I trigger the macro directly in Excel, the pivot table is built.这绝对不是这种情况,因为如果我直接在 Excel 中触发宏,则会构建数据透视表。 Alternatively, if I call a macro from QlikView to do anything (up to and excluding building the pivot table) it works.或者,如果我从 QlikView 调用宏来执行任何操作(直到并排除构建数据透视表),它也可以工作。
My VBScript:我的VBScript:
Set objExcelApp = CREATEOBJECT("Excel.Application")
Set objExcelSheet = objExcelApp.Worksheets("Sheet 1")
objExcelApp.Workbooks.Open "Test.xlsm"
objExcelApp.Application.Run "procBuildPivotReport"
objExcelSheet.SaveAs "Test2.xlsm"
SET objExcelSheet = NOTHING
SET objExcelApp = NOTHING
My VBA:我的 VBA:
Sub procBuildPivotReport()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws as Worksheet
Set ws as ActiveSheet
Dim pvtCache as PivotCache
Dim pvt as PivotTable
Set pvtCache = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("QV Data").Range("A1:D10"), Version:=6)
Set pvt = ws.PivotTables.Add(PivotCache, TableDestination:=Range("J1"), TableName:= "Sales Analysis")
With pvt
.PivotFields("Project ID").Orientation = xlRowField
.PivotFields("Name").Orientation = xlColumnField
.PivotFields("Hours").Orientation = xlDataField
Emd With
End Sub
Independently, the VBA works (when called directly inside Excel) and the VBScript works to send data and call an Excel Macro独立地,VBA 工作(当直接在 Excel 中调用时)和 VBScript 工作来发送数据和调用 Excel 宏
The issue appears to be when I try and combine the two and build a pivot table.问题似乎是当我尝试将两者结合起来并构建一个数据透视表时。
VBScript:脚本:
You can't get a sheet before opening the workbook.在打开工作簿之前您无法获得工作表。 You need to capture the workbook object so you can later close it.您需要捕获工作簿对象,以便稍后关闭它。 Add the workbook name to the .Run
call to ensure you get where you want to go.将工作簿名称添加到.Run
调用中,以确保您到达.Run
。 Close and quit before releasing the object handles.在释放对象句柄之前关闭并退出。
workbookPath = "Test.xlsm"
Set objExcelApp = CREATEOBJECT("Excel.Application")
Set thisWorkBook = objExcelApp.Workbooks.Open workbookPath
Set objExcelSheet = thisWorkBook.Worksheets("Sheet 1")
objExcelApp.Application.Run "'" & workbookPath & "'!procBuildPivotReport"
objExcelSheet.SaveAs "Test2.xlsm"
thisWorkBook.Close False
objExcelApp.Quit
Set objExcelSheet = Nothing
Set thisWorkBook = Nothing
Set objExcelApp = Nothing
VBA: VBA:
Set was missing "=".设置缺少“=”。 Use the pvtCache object you created in the pivot table.使用您在数据透视表中创建的 pvtCache 对象。 Add option explicit to catch typos.添加选项显式以捕获拼写错误。
Option Explicit
Sub procBuildPivotReport()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws as Worksheet
Set ws = wb.ActiveSheet
Dim pvtCache as PivotCache
Set pvtCache = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("QV Data").Range("A1:D10"), Version:=6)
Dim pvt as PivotTable
Set pvt = ws.PivotTables.Add(pvtCache, TableDestination:=Range("J1"), TableName:= "Sales Analysis")
With pvt
.PivotFields("Project ID").Orientation = xlRowField
.PivotFields("Name").Orientation = xlColumnField
.PivotFields("Hours").Orientation = xlDataField
Emd With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.