2017-08-02 1001 views
0

使用宏通過單擊按鈕創建數據透視表。我每天下載新的報告,因此每次都是不同的Excel工作表和工作表名稱。Excel調試VBA「運行時錯誤」424「:需要的對象」

我已經設法解決這個事實,即它是一個不同的工作簿名稱,每次只需重命名相關選項卡「數據」即可。我已經得到它創建我想要的數據透視表,除了它給我「計數」,而不是像我需要的「總和」。我得到的錯誤是

運行時錯誤「424」:
所需的對象

的亮的行是.Position = 1接近尾聲,我不知道怎麼在這裏打破。

Columns("A:A").Select 
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ 
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ 
    Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _ 
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _ 
    Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _ 
    ), Array(14, 1), Array(15, 1)) 
Cells.Select 
Sheets.Add 
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
    "data!R1C1:R1048576C15", Version:=xlPivotTableVersion14).CreatePivotTable _ 
    TableDestination:="Sheet1!R3C1", TableName:="PivotTable2", DefaultVersion _ 
    :=xlPivotTableVersion14 
Sheets("Sheet1").Select 
Cells(3, 1).Select 
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables(_ 
    "PivotTable2").PivotFields("Date"), "Count of Date", xlCount 
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables(_ 
    "PivotTable2").PivotFields("Store Listing Visitors"), _ 
    "Count of Store Listing Visitors", xlCount 
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables(_ 
    "PivotTable2").PivotFields("Installers"), "Count of Installers", xlCount 
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Date") 
    .Orientation = xlRowField 
    .Position = 1 
End With 
ExecuteExcel4Macro _ 
    "PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Store Listing Visitors"",,,2)" 
ExecuteExcel4Macro _ 
    "PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Installers"",,,2)" 

我很感謝您的幫助!

+4

請將代碼僅作爲文本格式化爲代碼塊(至少4個空格縮進)。圖像中的代碼很難被其他用戶複製和修復。 –

+0

請參閱:https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 – pnuts

+0

@Peh - hard is有點輕描淡寫!花時間寫出讀取圖像所需的OCR代碼。我不認爲我知道任何編碼人員需要15分鐘時間才能輸入代碼,而他們可以花幾周時間編寫OCR代碼。 :) –

回答

0

這裏是你的代碼

的重寫,我認爲你將有一個更容易調試的時候,如果你在一個更清晰的方式格式化你的代碼。

Sub pivTest() 

    Columns("A:A").TextToColumns _ 
     Destination:=Range("A1"), _ 
     DataType:=xlDelimited, _ 
     TextQualifier:=xlDoubleQuote, _ 
     ConsecutiveDelimiter:=False, _ 
     Tab:=True, _ 
     Semicolon:=False, _ 
     Comma:=True, _ 
     Space:=False, _ 
     Other:=False, _ 
     FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), _ 
         Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _ 
         Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), _ 
         Array(13, 1), Array(14, 1), Array(15, 1)) 

' Cells.Select ' do not use 

    Sheets.Add ' what is the name of this sheet ? 

    ActiveWorkbook.PivotCaches.Create(_ 
      SourceType:=xlDatabase, _ 
      SourceData:="data!R1C1:R1048576C15", _ 
      Version:=xlPivotTableVersion14).CreatePivotTable _ 
                TableDestination:="Sheet2!R3C1", _ 
                TableName:="PivotTable2", _ 
                DefaultVersion:=xlPivotTableVersion14 
' Sheets("Sheet1").Select ' do not use 

' Cells(3, 1).Select  ' do not use 

    Dim pt As PivotTable 
    Set pt = Sheets("Sheet1").PivotTables("PivotTable2") 

    pt.AddDataField pt.PivotFields("Date"), "Count of Date", xlCount 
    pt.AddDataField pt.PivotFields("Store Listing Visitors"), "Count of Store Listing Visitors", xlCount 
    pt.AddDataField pt.PivotFields("Installers"), "Count of Installers", xlCount 

    pt.PivotFields("Count of Date").Orientation = xlRowField 
    pt.PivotFields("Date").Position = 1   ' it is not "Count of Date" anymore, because previous line moved it out of the "Sum Values" 

    ExecuteExcel4Macro "PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Store Listing Visitors"",,,2)" 
    ExecuteExcel4Macro "PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Installers"",,,2)" 

End Sub 
相關問題