2017-08-02 1006 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 
相关问题