VBA中的数据透视表代码只能运行一次

时间:2018-05-10 17:48:39

标签: excel vba excel-vba

我有一张1532行和7列的表格,名为"延迟学生"。我想在工作表中制作一个数据透视表,但每次我使用我的代码时,我都会得到一个

  

运行时错误' 438"对象不支持此属性或方法"

此区域为黄色(.Position = 1):

With .PivotFields("FACULTY_ID")
  .Orientation = xlDataField
  .Postion = 1

但是无论如何它都会创建数据透视表。问题是,当我删除表并尝试再次使用代码时,它不会再次创建数据透视表。所以我的代码一定有问题:

Sub Pivot1()

Dim PvtTbl As PivotTable
Dim PvtCache As PivotCache

Dim PvtTblName As String
Dim pivotTableWs As Worksheet

PvtTblName = "pivotTableName"

Set pivotTableWs = Sheets("Delayed Students")


Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Delayed Students!R1C1:R1532C7")


Set PvtTbl = pivotTableWs.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=pivotTableWs.Range("J1"), TableName:=PvtTblName)

With PvtTbl
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = False
.RowAxisLayout xlCompactRow

With .PivotCache
    .RefreshOnFileOpen = False
    .MissingItemsLimit = xlMissingItemsDefault
End With

.RepeatAllLabels xlRepeatLabels

With .PivotFields("STUDYBOARD_ID")
    .Orientation = xlRowField
    .Position = 1
End With

With .PivotFields("FACULTY_ID")
  .Orientation = xlDataField
  .Postion = 1


End With
End With



End Sub

希望有人可以帮我解决我做错的事情:)。

0 个答案:

没有答案