繁体   English   中英

如何修复Excel VBA中的“无法设置PivotField类的Position属性”错误

[英]How to fix 'Unable to set the Position property of the PivotField class' error in Excel VBA

我正在使用VBA创建数据透视表,并使用For Each循环从动态数组中插入行字段(使用Application.Match设置位置)。 如何解决该位置错误?

Run-time error '1004':
Unable to set the Position property of the PivotField class

我认为.position的p可能区分大小写,因为当我在线搜索时它是大写的,但是Excel VBA编辑器会将其纠正为小写。 编辑:因为我以前宣布position为变量,我最终报废了。 声明Position修复。 但是,错误仍然存​​在

Sub ReportPivot ()
'
' ReportPivot Macro
'

' Deactivate ScreenUpdating
    Application.ScreenUpdating = False

' Declare Variables
    Dim DataSheet As Worksheet
    Dim LastRow As Long
    Dim LastCol As Long
    Dim DataRange As Range

    Dim PSheet As Worksheet
    Dim PCache As PivotCache
    Dim PTable As PivotTable
    Dim PField As PivotField

' Name Data Sheet
    ActiveSheet.Name = "Data"
' Define DataSheet
    Set DataSheet = Worksheets("Data")

' Define DataRange
    Set DataRange = DataSheet.Cells(1, 1).CurrentRegion

' Add the Processed Report sheet
    Set PSheet = ActiveWorkbook.Worksheets.Add
    PSheet.Name = "Processed Report"

' Insert DataPivotTable
    Set PTable = PSheet.PivotTableWizard(SourceType:=xlDatabase, _
    SourceData:=DataRange, _
    TableDestination:=PSheet.Cells(2, 2), _
    TableName:="DataPivotTable", _
    RowGrand:=False, _
    ColumnGrand:=False)

' Set to Tabular Layout
    PTable.RowAxisLayout xlTabularRow

' Insert Row Fields
    Dim RowFields As Variant
    RowFields = Array("productid", "Start Date", "End Date", "OriginalBlock", "ReleaseDays", "ReleaseDate")

    Dim field
    For Each field In RowFields
        Set PField = PTable.pivotFields(field)
        With PField
            .Orientation = x1RowField
            .position = Application.Match(field, RowFields, False)
            .Subtotals(place) = False
        End With
    Next field

' Ungroup Date Fields
    Dim DateFields As Variant
    DateFields = Array("Start Date", "End Date", "ReleaseDate")

    Dim dfield
    For Each dfield In DateFields
        Set PField = PTable.pivotFields(dfield)
        PField.DataRange.Cells(1).Ungroup
    Next dfield

' Sort oldest to newest
    PTable.pivotFields("Start Date").AutoSort _
        xlAscending, "Start Date"

' Resize columns
    Cells.EntireColumn.AutoFit

' Deactivate repeat labels
   PTable.RepeatAllLabels xlDoNotRepeatLabels

' Reactivate ScreenUpdating
    Application.ScreenUpdating = True

end sub

以下是一些示例数据(csv):

productid,OriginalBlock,ReleaseDays,ReleaseDate,Start Date,End Date
JRS,16,6,,5/1/2019,5/31/2019
JRS,16,6,,5/1/2020,5/31/2020
JRS,16,8,,6/26/2020,6/27/2020
JRS,16,15,,6/28/2020,7/25/2020
JRS,1,22,,7/26/2020,8/25/2020
MAS,11,6,,5/1/2019,5/31/2019
MAS,11,8,,6/1/2019,6/27/2019
MAS,11,15,,6/28/2019,7/25/2019
MAS,1,22,,7/26/2019,8/25/2019
MAS,11,15,,8/26/2019,9/8/2019
MAS,11,8,,9/9/2019,9/30/2019
MAS,11,6,,10/1/2019,10/5/2019
MAS,11,6,,5/1/2020,5/31/2020
MAS,1,22,,7/26/2020,8/25/2020
SUP,25,6,,5/1/2019,5/31/2019
SUP,25,8,,6/1/2019,6/27/2019
SUP,25,15,,6/28/2019,7/25/2019
SUP,1,22,,7/26/2019,8/25/2019
SUP,25,15,,8/26/2019,9/8/2019
SUP,25,8,,9/9/2019,9/30/2019
SUP,25,6,,10/1/2019,10/5/2019
SUP,25,6,,5/1/2020,5/31/2020
SUP,25,8,,6/1/2020,6/24/2020
SUP,25,15,,6/25/2020,7/25/2020
SUP,1,22,,7/26/2020,8/25/2020
SUP,25,15,,8/26/2020,9/8/2020
SUP,25,8,,9/9/2020,9/30/2020
SUP,25,6,,10/1/2020,10/6/2020

答案是正如Tim所指出的那样是错别字: x1RowFieldxlRowField 应该是后者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM