运行时错误1004:应用程序定义或对象定义的错误

时间:2012-10-25 07:12:03

标签: excel vba excel-vba

ExcelSpreadSheet VBAScrennshot我正在尝试录制一个宏,它将从excel数据中创建数据透视图,这里是已录制的代码:

Sub chart1()
 '
 ' chart1 Macro
 '

 '
Range("E1:F11").Select
Sheets.Add
In Debugger, code within the **** **** is shown in Yellow color
***** ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "data!R1C5:R11C6", Version:=xlPivotTableVersion12).CreatePivotTable _
    TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion12 ********
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("question1")
    .Orientation = xlRowField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
    .Orientation = xlColumnField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("answer1"), "Count of answer1", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
    .Orientation = xlPageField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1").Orientation = _
    xlHidden
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
    .Orientation = xlColumnField
    .Position = 1
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:$D$6")
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:$D$6")
ActiveChart.ChartType = xlColumnClustered
End Sub

当我尝试运行此宏时,为什么会出现运行时错误1004:应用程序定义错误或对象定义错误?

提前致谢..

1 个答案:

答案 0 :(得分:1)

由于您希望图表位于新工作表上, 你必须将宏的“Sheet1”更改为新工作表的名称, 以下宏应该适合您,我已将新工作表命名为newWs

并且fyi,我认为你的宏的错误消息是由于试图在“Sheet1”上创建2个同名的数据透视表是不允许的。

他还想知道在Selected Area上创建pivotTable的内容,所以我对代码进行了修改。

已编辑:我假设您每次都选择2列

Sub chart1()
 '
 ' chart1 Macro
 '

 '
Dim selectedSheetName As String
Dim newWs As Worksheet
Dim rangeName As String
Dim header1 As String
Dim header2 As String
header1 = ActiveSheet.Cells(1, Selection.Column).Value
header2 = ActiveSheet.Cells(1, Selection.Column + 1).Value
selectedSheetName = ActiveSheet.Name
rangeName = Selection.Address
Set newWs = Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    selectedSheetName & "!" & rangeName, Version:=xlPivotTableVersion12).CreatePivotTable _
    TableDestination:=newWs.Name & "!R3C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion12
newWs.Activate
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header1)
    .Orientation = xlRowField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
    .Orientation = xlColumnField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields(header2), "Count of answer1", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
    .Orientation = xlPageField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields(header2).Orientation = _
    xlHidden
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
    .Orientation = xlColumnField
    .Position = 1
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(newWs.Name & "!$A$3:$D$6")
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(newWs.Name & "!$A$3:$D$6")
ActiveChart.ChartType = xlColumnClustered
End Sub