首页 > 解决方案 > 运行时错误“1004”无法获取数据透视表类的 PivotFields 属性

问题描述

我开发了一个宏,它会自动生成报告,但使用某种类型的数据我收到了错误。我尝试了几件事,但我没有设法解决它,所以我寻求一些帮助。,这是有问题的行:Fin = .PivotTables(1).PivotFields("Account Owner").VisibleItems.Count这是完整的代码:

Sub Generar_informes()

Dim i As Double
Dim Ini As Double
Dim Fin As Double

'Desactivamos actualización de pantalla
Application.ScreenUpdating = False
Sheets("Pivot Table").Select
With Sheets("Pivot Table")

'indicamos la fila siguiente a la primera fila con datos
Ini = Columns(1).Range("A1").End(xlDown).Row
'Contamos los todas los comerciales de la tabla dinámica1
Fin = .PivotTables(1).PivotFields("Account Owner").VisibleItems.Count

'Inciamos un bucle donde por cada comercial generamos informe
For i = 1 To Fin
'para mostrar el informe debemos hacer referencia a los campos de valores
.Cells(i + Ini, 2).ShowDetail = True
'Nombramos la pestaña con el nombre del comercial.
ActiveSheet.Name = .Cells(i + Ini, 1).Value
'Seleccionamos la hoja con el detalle del comercial
ActiveSheet.Select
'Movemos la hoja a un libro nuevo
ActiveSheet.Move
'Cambiamos color de celdas a modificar por los comerciales
Range("L1:N1").Interior.Color = RGB(255, 153, 0)
'Agregamos la lista desplegable
    Columns("N:N").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="Director,Manager,Owner,Staff,Top Executive(Chairman-Chief-etc),Vice President/SVP/EVP"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With



ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & ActiveSheet.Name

ActiveWorkbook.Close False
Next i
End With
Application.ScreenUpdating = True
MsgBox "¡Reportes generados exitosamente!"
End Sub

谢谢先进

标签: excelvba

解决方案


推荐阅读