0
進出口試圖創建通過VBA樞軸圖表(因此,基於從一個形式的動態值的按鈕可以創建餅圖)創建透視圖表使用VBA導致運行5錯誤
我的代碼是:
Dim iRow As Long
'//Find First Empty Row In Database
iRow = Sheets("search results").Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row
Sheets("Custom Chart").visible = True
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Search Results!A3:AM" & iRow, Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Custom Chart!A1", TableName:="PivotTable6" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Custom Chart").Select
Cells(1, 1).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range("Custom Chart!$A$1:$C$18")
ActiveSheet.Shapes("Chart 1").IncrementLeft 192
ActiveSheet.Shapes("Chart 1").IncrementTop 15
ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables(_
"PivotTable6").PivotFields("Ethnicity Of Child"), "Count of Ethnicity Of Child" _
, xlCount
With ActiveSheet.PivotTables("PivotTable6").PivotFields(Me.Dy4.Value)
.Orientation = xlRowField
.Position = 1
End With
ActiveChart.ChartType = xlPie
ActiveChart.ApplyLayout (6)
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Chart Result"
ActiveWorkbook.ShowPivotTableFieldList = False
我的代碼失敗,在這條線:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Search Results!A3:AM" & iRow, Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Custom Chart!A1", TableName:="PivotTable6" _
, DefaultVersion:=xlPivotTableVersion14
說已經發生了運行時5錯誤。我能想到的唯一原因是我試圖使用單元格引用來定義一個範圍,我注意到如果您創建一個透視圖表,它使用範圍如Sheet1!R1C1
,但我不明白這些引用。
任何幫助,將不勝感激。
在此先感謝。
iRow的類型爲Long,並且您試圖將其添加到字符串中。您需要用'CStr(iRow)'替換'iRow'。也檢查出這個話題http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select/10717999#10717999。 – DeanBDean
@DeanBDean不幸的是,你使用'CStr(iRow)'的建議不能解決問題。另外,在其他地方,我將iRow定義爲一個長期使用它,並且以相同的方式使用它,而不存在問題。 – SilverShotBee
我相信你的問題是在TableDestination上:=「Custom Chart!A1」。當我用範圍對象替換「Custom Chart!A1」時,我遇到了Runtime 5錯誤。在創建數據透視表的行之前,添加'Dim pivotDest as Range'。然後在下一行添加'Set pivotDest = ActiveWorkbook.Sheets(「Custom Chart」)。Range(「A1」)'。然後用'pivotDest'替換''Custom Chart!A1'' – DeanBDean