簡體   English   中英

Excel調試VBA“運行時錯誤'424':必需對象”

[英]Excel debugging VBA “Run-time error '424': Object required”

單擊按鈕,使用宏來創建數據透視表。 我每天都下載新報告,因此每次都有不同的Excel工作表和工作表名稱。

通過每次僅重命名相關的選項卡“數據”,我已經設法解決了它是一個不同的工作簿名稱的事實。 我已經得到它來創建我想要的數據透視表,除了它像我需要的那樣是給我“ Count”而不是“ Sum”。 我得到的錯誤是

運行時錯誤“ 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)"

感謝您的幫助!

這是您的代碼的重寫

我認為,如果您以更清晰的方式格式化代碼,則調試起來會更輕松。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM