任何人都可以看到爲什麼此代碼會導致最後一行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
謝謝!那個錯誤消失了。現在我已經有了另外的1004個,只是比之前幾行還多。我相信這可能類似。留意我的下一篇文章。 :) – user2021539 2013-05-02 12:10:19