1

私はVBAが初めてです。マクロを記録し、それを編集したいと思いました。録音した後、一度実行したいと思いました。しかし、実行すると、ランタイム エラー 5 が返されました。

マクロはシートから取得し、別のシートのピボットテーブルに追加する必要があります。

したがって、これはエラーが基づいているコードです。

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "sourcetable!R1C1:R1048576C21", Version:=xlPivotTableVersion14). _
    CreatePivotTable TableDestination:="Tabelle2!R3C1", TableName:= _
    "PivotTable1", DefaultVersion:=xlPivotTableVersion14

ご協力いただきありがとうございます

4

2 に答える 2

1

いいえ、それはこのピボット専用の新しいシートではありません @SiddharthRout – 初心者 4 分前

最も簡単な方法は、

With ActiveWorkbook
    .Sheets("Tabelle2").Cells.Clear

    .PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "sourcetable!R1C1:R1048576C21", Version:=xlPivotTableVersion14). _
    CreatePivotTable TableDestination:="Tabelle2!R3C1", TableName:= _
    "PivotTable1", DefaultVersion:=xlPivotTableVersion14
End With

また、行 1048576 までソース データが定義されていることに気付きました。なぜですか? より完璧な方法は、最後の行を見つけてから範囲を構築することです。例えば

Sub Sample()
    Dim lRow As Long

    With ActiveWorkbook
        '~~> Find last row in sheet sourcetable
        With .Sheets("sourcetable")
            If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
                lRow = .Cells.Find(What:="*", _
                              After:=.Range("A1"), _
                              Lookat:=xlPart, _
                              LookIn:=xlFormulas, _
                              SearchOrder:=xlByRows, _
                              SearchDirection:=xlPrevious, _
                              MatchCase:=False).Row
            Else
                lRow = 1
            End If
        End With

        .Sheets("Tabelle2").Cells.Clear

        .PivotCaches.Create(SourceType:=xlDatabase, _
                            SourceData:="sourcetable!R1C1:R" & _
                                                        lRow & _
                                                        "C21", _
                            Version:=xlPivotTableVersion14).CreatePivotTable _
                            TableDestination:="Tabelle2!R3C1", _
                            TableName:="PivotTable1", _
                            DefaultVersion:=xlPivotTableVersion14
    End With
End Sub
于 2013-09-20T10:17:09.060 に答える
0

最初: sourcetable と Tabelle2 ワークシートは存在しますか?

次に、オプションの意味のない引数をすべて削除してみてください。

ActiveWorkbook.PivotCaches.Create(xlDatabase, "sourcetable!R1C1:R1048576C21") _
    .CreatePivotTable ActiveWorkbook.Worksheets("Tabelle2").Range("R3C1")
于 2013-09-20T10:13:49.170 に答える