2016-05-26 86 views
1

的樞軸數據集我的目的是創建一個數據透視表,並在轉儲數據(A1:AE170000)中創建一個數據透視表。我在下面附加了我的代碼,如果我將數據減少到60-65k行左右,則完美無缺,但不會。代碼不適用於行數> 65536

它引發運行時錯誤13:類型不匹配在我設置我的數據透視表緩存(PTCache)的行。

Private Sub OptionButton3_Click() 
    Application.ScreenUpdating = False 
    Application.Calculation = xlCalculationManual 
    ThisWorkbook.Sheets("Data").Activate 

    Dim PTCache As PivotCache 
    Dim PT As PivotTable 
    'Setting range as my entire data set 
    Dim PTRange As Range 
    Set PTRange = Range("A1", Range("A1").End(xlToRight).End(xlDown)) 
    'Adding a new worksheet for Pivot Table and Chart 
    Dim ws As Worksheet 
    Set ws = Sheets.Add 
    ws.Name = "All" 
    PTRange.Select 
    ThisWorkbook.Sheets("All").Activate 
    'Runtime error 13:Type Mismatch at this line while setting PTCache 
    Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, PTRange) 
    Set PT = ActiveSheet.PivotTables.Add(PTCache, Range("A1"), "All") 
    With PT 
     .PivotFields("Name").Orientation = xlPageField 
     .PivotFields("Rate").Orientation = xlDataField 
     .PivotFields("Date").Orientation = xlRowField 
    End With 
    PT.PivotSelect ("") 

    Charts.Add 
    ActiveChart.Location where:=xlLocationAsObject, Name:=PT.Parent.Name 
    ActiveChart.ChartType = xlLine 
    ActiveChart.Parent.Top = Range("I7").Top 
    ActiveChart.Parent.Left = Range("I7").Left 

    Range("A2").Select 
    Application.Calculation = xlCalculationAutomatic 
    Application.ScreenUpdating = True 
End Sub 
+0

您使用的是哪個excel – EoinS

+0

當剛剛添加的新工作表處於活動狀態時,如何從數據工作表中選擇PTRange? – Jeeped

+0

我正在使用Excel 2007.我在設置並選擇PTRange後激活了新工作表。 –

回答

1

PivotCaches.Create Method (Excel)(我的重點):> <等等> ...當路過的範圍,建議要麼使用字符串指定工作簿

<等等,工作表和單元格區域,或者設置一個命名區域並將該名稱作爲字符串傳遞。 傳遞Range對象可能會導致意想不到的「類型不匹配」錯誤

只需設置一個字符串var到數據工作表的外部地址Range.CurrentRegion property從A1輻射出來並使用它。

Option Explicit 

Private Sub OptionButton3_Click() 
    'Application.ScreenUpdating = False 
    Application.Calculation = xlCalculationManual 

    Dim PT As PivotTable, PTCache As PivotCache 
    Dim PTRange As Range, ws As Worksheet, strRNG As String 

    strRNG = ThisWorkbook.Worksheets("Data").Cells(1, 1).CurrentRegion.Address(external:=True) 

    With Worksheets.Add(after:=Sheets(Sheets.Count)) 
     .Name = "All" 
     Set PTCache = .Parent.PivotCaches.Create(xlDatabase, strRNG) 
     Set PT = .PivotTables.Add(PTCache, .Range("A1"), "All") 
     With PT 
      .PivotFields("Name").Orientation = xlPageField 
      .PivotFields("Rate").Orientation = xlDataField 
      .PivotFields("Date").Orientation = xlRowField 
     End With 
     PT.PivotSelect ("") 
    End With 

    'all the chart stuff here 

    Application.Calculation = xlCalculationAutomatic 
    Application.ScreenUpdating = True 
End Sub 
+0

非常感謝!它的工作... –

+0

[聯盟](https://msdn.microsoft.com/en-us/library/office/ff834621.aspx)我想你的意思是不連續的列。建立聯合並以[Range.Address External:= True](https://msdn.microsoft.com/en-us/library/office/ff837625.aspx)作爲字符串來嘗試。如果這不起作用,請將代碼中的一些數據發佈到新問題中。這是一個很好的問題。不希望它成爲[俄羅斯娃娃問題](http://meta.stackexchange.com/questions/188625)。 – Jeeped

相關問題