2

このコードが最後の行で 1004 エラーを引き起こす理由を誰でも理解できますか? その最後の行まで、すべてがうまく機能します。私はそれを機能させましたが、このエラーが発生し始めましたが、その理由がわかりません。Sheet2 は空白のシートです。Sheet1 は現在、10 行 3 列のテスト データです。B3から始まります。誰にもアイデアはありますか?

    Sub CreatePivot()
        ' Define RngTarget and RngSource as Range type variables
        Dim RngTarget As Range
        Dim RngSource As Range
        Dim intLastCol As Integer
        Dim intCntrCol As Integer

        ' RngTarget is where the PivotTable will be created (ie: Sheet2, Cell B3)
        Set RngTarget = ThisWorkbook.Worksheets("Sheet2").Range("B3")

        ' RngSource defines the Range that will be used to create the PivotTable
        ' ActiveWorkbook = The currently opened Workbook
        ' ActiveSheet = The currectly opened sheet
        ' UsedRange = The Range of cells with active data in them
        Set RngSource = ActiveWorkbook.ActiveSheet.UsedRange

        ' Select the Range
        RngSource.Select

        ' Copy the Range into the clipboard
        RngSource.Copy

        ' Create a new PivotTable using the RngSource defined above,
        ' in Excel format,
        ' placed at the RngTarget location,
        ' And name it PivotB3 just for reference if needed
        ActiveWorkbook.PivotCaches.Create(xlDatabase, RngSource).CreatePivotTable RngTarget, "PivotB3"

        ' Get the last used column from the data table
        intLastCol = RngSource.Columns(RngSource.Columns.Count).Column

        ' Select the Pivot table so we can apply the conditional formats
        ActiveSheet.PivotTables("PivotB3").PivotSelect "", xlDataAndLabel, True
4

2 に答える 2

1

ピボット テーブルがアクティブ シートではなくシート 2 にあるため、エラーが発生しています。ピボット テーブルを選択する前に sheet2 を選択するだけでエラーを修正できますが、本当にやりたいことは、コードからすべての選択を削除することです。詳細な説明/例については、Select の使用を避けるを参照してください。

これを試して:

Sub CreatePivot()
        Dim RngTarget As Range
        Dim RngSource As Range
        Dim ws As Worksheet
        Dim pt As PivotTable

        Set ws = ThisWorkbook.Sheets("Sheet2")
        ws.Cells.Clear
        ' RngTarget is where the PivotTable will be created (ie: Sheet2, Cell B3)
        Set RngTarget = ws.Range("B3")

        Set RngSource = ActiveWorkbook.ActiveSheet.UsedRange

        ' Create a new PivotTable
        ActiveWorkbook.PivotCaches.Create(xlDatabase, RngSource).CreatePivotTable _
            RngTarget, "PivotB3"
        Set pt = RngTarget.PivotTable

        ' We now have access to the pivot table and can modify as needed
        pt.PivotSelect "", xlDataAndLabel, True
        'ActiveSheet.PivotTables("PivotB3").PivotSelect "", xlDataAndLabel, True
End Sub

注:何が起こっているかを簡単に確認できるように、問題の一部ではない変数とコメントを削除しました。

于 2013-05-01T12:32:33.760 に答える
0

以下のコードを試してください:

Sub CreatePivot()
' Define RngTarget and RngSource as Range type variables
    Dim RngTarget As Range
    Dim RngSource As Range
    Dim intLastCol As Integer
    Dim intCntrCol As Integer

    ' RngTarget is where the PivotTable will be created (ie: Sheet2, Cell B3)
    Set RngTarget = ThisWorkbook.Worksheets("Sheet2").Range("B3")

    ' RngSource defines the Range that will be used to create the PivotTable
    ' ActiveWorkbook = The currently opened Workbook
    ' ActiveSheet = The currectly opened sheet
    ' UsedRange = The Range of cells with active data in them
    Set RngSource = ActiveSheet.UsedRange

    ' Select the Range
   ' RngSource.Select

    ' Copy the Range into the clipboard
   ' RngSource.Copy

    ' Create a new PivotTable using the RngSource defined above,
    ' in Excel format,
    ' placed at the RngTarget location,
    ' And name it PivotB3 just for reference if needed
    Dim oPC As PivotCache
   Set oPC = ActiveWorkbook.PivotCaches.Create(xlDatabase, RngSource)

   Dim oPT As PivotTable
   Set oPT = oPC.CreatePivotTable(RngTarget, "PivotB3", True)

    ' Get the last used column from the data table
    intLastCol = RngSource.Columns(RngSource.Columns.Count).Column

    ' Select the Pivot table so we can apply the conditional formats
    'Worksheets("Sheet2").PivotTables("PivotB3").PivotSelect "", xlDataAndLabel, True
End Sub
于 2013-05-01T03:32:50.997 に答える