错误13类型不匹配和错误9脚本超出范围

时间:2014-04-28 19:58:48

标签: excel

我创建了一个类似的宏来生成数据透视表和图表,它运行正常。现在我需要创建一个新的,所以我应用相同的代码,但相应地修改它。但是这一次,VBA告诉我,我有错误9'脚本超出范围,有时错误13'类型不匹配我执行代码后。我重复运行代码几次,但有时它给出了错误13,有时它给出了错误9.所以我很困惑。代码与我以前的工作几乎相同。我想知道是否有人可以指出我出错的地方。我很感激。谢谢!

Sub Diff_Milg()
Dim SourceRange As Range
Dim ObjField As PivotField
Dim objPivot As PivotTable
Dim LastRow As Long
Dim lblRng As Range
Dim grRng As Range

LastRow = Cells(Rows.Count, 1).End(xlDown).Row


Sheets("Sheet1").Select



Set SourceRange = Sheets("Sheet1").Range("A2:D" & LastRow)

Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Different Milg"

ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=SourceRange).CreatePivotTable _
TableDestination:=Sheets("Different Milg").Range("A1"), _
TableName:="Different Mileage", _
DefaultVersion:=xlPivotTableVersion11    *Error Happens here

ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(1, 1)

With ActiveSheet.PivotTables("Different Mileage").PivotFields("ProductionYM")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Different Mileage").PivotFields("Milg")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("Different Mileage").PivotFields("FailPartId")
.Orientation = xlDataField
.Name = "CountofMilg"
 .Function = xlCount
.Position = 1
End With
Set objPivot = ActiveSheet.PivotTables("Different Mileage")
'Set range of field label whose items are to be grouped:
Set lblRng = objPivot.PivotFields("Milg").LabelRange
lblRng.Group Start:=0, End:=36000, by:=5000
 'to ungroup:
 'lblRng.Ungroup

 'Set Range of dates to be grouped
 Set grRng = objPivot.PivotFields("ProductionYM").DataRange

0 个答案:

没有答案