Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 数据透视表错误1004_Vba_Excel_Pivot Table - Fatal编程技术网

Vba 数据透视表错误1004

Vba 数据透视表错误1004,vba,excel,pivot-table,Vba,Excel,Pivot Table,有人知道为什么这个代码会在最后一行导致1004错误吗?直到最后一行,一切都很好。我让它工作,然后它开始得到这个错误,我不知道为什么。表2是空白表。Sheet1目前只是测试数据,10行3列。它从B3开始。有人有什么想法吗 Sub CreatePivot() ' Define RngTarget and RngSource as Range type variables Dim RngTarget As Range Dim RngSource

有人知道为什么这个代码会在最后一行导致1004错误吗?直到最后一行,一切都很好。我让它工作,然后它开始得到这个错误,我不知道为什么。表2是空白表。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
请尝试以下代码:

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

出现错误是因为数据透视表位于sheet2上,而不是activesheet上。您可以通过在选择透视表之前简单地选择sheet2来修复错误,但您真正想要做的是从代码中删除所有选择。有关更多说明/示例,请参阅

试试这个:

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

注意:我已经删除了不属于您的问题的变量和注释,以便更容易看到发生了什么。

谢谢!这个错误消失了。现在我又有了1004条,比上一条只多了几行。我相信可能是类似的。注意我的下一篇文章。:)