创建数据透视表

时间:2011-03-12 01:02:46

标签: excel-vba vba excel

我得到此运行时错误'1004': - 此命令至少需要两行源数据。您不能仅在一行中的选择上使用该命令。请尝试以下操作:如果您使用的是高级过滤器,请选择包含至少两行数据的单元格范围。然后再次单击“高级筛选”命令。如果您要创建数据透视表或数据透视图报表,请键入[它在此处停止]

我收到了上述错误: -

pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12

我正在尝试运行此代码: -

Sub CreateTable()
Dim lastRow As Long
Dim pvc As PivotCache
 lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
    ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Set pvc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Database", Version:=xlPivotTableVersion12)
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
    Sheets("TABLE").Select
    Cells(1, 1).Select
    ActiveWorkbook.ShowPivotTableFieldList = True
    With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
    "INFO").PivotFields("GRADE"), "Sum of GRADE", xlSum
    ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
    "INFO").PivotFields("SIZE"), "Sum of SIZE", xlSum
    ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
    "INFO").PivotFields("QTY"), "Sum of QTY", xlSum
    With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
        .Orientation = xlColumnField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
        .Orientation = xlColumnField
        .Position = 3
    End With
    With ActiveSheet.PivotTables("INFO").PivotFields("Sum of GRADE")
        .Orientation = xlPINFOField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("INFO").PivotFields("Sum of SIZE")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveWorkbook.ShowPivotTableFieldList = False
    Range("B3").Select
    ActiveSheet.PivotTables("INFO").CompactLayoutColumnHeader = "MODEL"
    Range("A5").Select
    ActiveSheet.PivotTables("INFO").CompactLayoutRowHeader = "SIZE"
    Range("A1").Select
    ActiveSheet.PivotTables("INFO").PivotFields("GRADE").Caption = "GRADE"
    Cells.Select
    Cells.EntireColumn.AutoFit
    Columns("B:BB").Select
    Selection.Style = "Comma"
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("C1").Select
End Sub

请帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

出现此错误的原因似乎是第四行:

lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row

通过这样的表示,您可以通过查看第6列找到“DATA”表中的最后一行数据。现在,如果我们看一下您将其与之结合起来:

ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow

现在,如果你的源数据在单元格G1:K **中,那么:

ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
除非第6列中有数据,否则

将始终返回1,但从上面一行开始,您的数据似乎从第7列(“G”)开始。因此,要使其工作,您必须将第四行更改为:

lastRow = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row