2013-05-01 133 views
2

任何人都可以看到为什么此代码会导致最后一行1004错误?一切都很好,直到最后一行。我有它的工作,然后开始得到这个错误,我不明白为什么。 Sheet2是一张空白纸。 Sheet1目前只是测试数据,10行,3列。它始于B3。有人有主意吗?数据透视表错误1004

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 

回答

1

你所得到的错误,因为数据透视表是在Sheet2上,而不是activesheet。您可以通过在选择数据透视表之前选择sheet2来修复错误,但是您真正想要做的是消除代码中的所有选择。进一步的解释/例子见Avoid Using 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 

注:是不是你的问题的一部分我已经删除变量和意见,使之更容易看到发生了什么。

+0

谢谢!那个错误消失了。现在我已经有了另外的1004个,只是比之前几行还多。我相信这可能类似。留意我的下一篇文章。 :) – user2021539 2013-05-02 12:10:19

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