ドライブから Excel ファイルを開いてピボット テーブルを作成しようとしています。
しかし、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というエラーが表示されます
以下にコードを添付しました
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim excelApplication As Microsoft.Office.Interop.Excel.Application = Nothing
Dim excelWorkBook As Workbook = Nothing
Dim targetSheet As Worksheet = Nothing
Dim pivotTable As PivotTable = Nothing
Dim pivotData As Ranges = Nothing
Dim pivotDestination As Range = Nothing
Dim co As PivotField = Nothing
Dim pivotTableName As String = "Complexity"
excelApplication.Workbooks.Open("E:\John\ccc.xlsx")
pivotData = targetSheet.Range("Data!R1C1:R11460C9")
pivotDestination = targetSheet.Range("Data!R1C11")
excelWorkBook.PivotTableWizard( _
XlPivotTableSourceType.xlDatabase, _
pivotData, pivotDestination, pivotTableName, True, True, _
True, True, , , False, False, XlOrder.xlDownThenOver, 0)
With excelApplication.ActiveSheet.PivotTables("Complexity").PivotFields("Com")
End With
excelApplication.ActiveSheet.PivotTables("Complexity").AddDataField(excelApplication.ActiveSheet.PivotTables( _
"Complexity").PivotFields("Per gram"), "Sum of Per gram", XlConsolidationFunction.xlSum)
excelApplication.ActiveSheet.PivotTables("Complexity").AddDataField(excelApplication.ActiveSheet.PivotTables( _
"Complexity").PivotFields("Oracle Per gram"), "Sum of Oracle Per gram", XlConsolidationFunction.xlSum)
excelApplication.ActiveWorkbook.Close()
excelApplication.Quit()
End Sub