Hi,
I wrote a macro that creates a pivot table and a chart.
It works correctly when I run it with the data in the same workbook that I created the macro. But when i try to run the macro with another workbook. It gives me an error.
The error says "reference is not valid."
Sub Macro1()
ThisWorkbook.Names.Add Name:="NewName", _
RefersTo:="=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))", Visible:=True
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"NewName"
.CreatePivotTable TableDestination:="", TableName:="PivotTable1"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1"
.SmallGrid = False
With ActiveSheet.PivotTables("PivotTable1"
.PivotFields("sexfmt"
.Orientation = xlDataField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1"
.PivotFields("acc"
.Orientation = xlDataField
.Position = 2
End With
Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.SetSourceData Source:=Sheets("Sheet2"
.Range("A3"
End Sub
The error in the code is in bold. I am using excel 2000. I want to be able to use the macro for any workbook to create a pivote table. Any help would be appreciated.
Thanks,
Mike
I wrote a macro that creates a pivot table and a chart.
It works correctly when I run it with the data in the same workbook that I created the macro. But when i try to run the macro with another workbook. It gives me an error.
The error says "reference is not valid."
Sub Macro1()
ThisWorkbook.Names.Add Name:="NewName", _
RefersTo:="=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))", Visible:=True
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"NewName"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1"
With ActiveSheet.PivotTables("PivotTable1"
.Orientation = xlDataField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1"
.Orientation = xlDataField
.Position = 2
End With
Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.SetSourceData Source:=Sheets("Sheet2"
End Sub
The error in the code is in bold. I am using excel 2000. I want to be able to use the macro for any workbook to create a pivote table. Any help would be appreciated.
Thanks,
Mike