Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何调试透视图宏?_Vba_Excel_Macros_Pivot Chart - Fatal编程技术网

Vba 如何调试透视图宏?

Vba 如何调试透视图宏?,vba,excel,macros,pivot-chart,Vba,Excel,Macros,Pivot Chart,我无法运行此操作,我想计算工作表中的总行数,并将其传递给pivot图表进行创建 透视图创建 选择文件 双击“总计”以创建新的电子表格 下面的代码检查工作表1中的数据(修改工作表名称),并在工作表报告中创建透视表和图表 在第一次创建数据透视表和图表时,从第二次开始,它仅使用更新的数据行(在Sheet1中)刷新数据透视缓存并更新图表 Sub Macro2() Dim sht1 As Worksheet Dim shtReport

我无法运行此操作,我想计算工作表中的总行数,并将其传递给pivot图表进行创建

  • 透视图创建
  • 选择文件
  • 双击“总计”以创建新的电子表格

  • 下面的代码检查工作表1中的数据(修改工作表名称),并在工作表报告中创建透视表和图表

    在第一次创建数据透视表和图表时,从第二次开始,它仅使用更新的数据行(在Sheet1中)刷新数据透视缓存并更新图表

    Sub Macro2()
    
    Dim sht1                            As Worksheet
    Dim shtReport                       As Worksheet
    Dim lastRow                         As Long
    Dim PivotSrc_Range                  As Range
    
    Dim PvtCache                        As PivotCache
    Dim PvtTbl                          As PivotTable
    Dim Chart1                          As Chart
    
    ' modify to your sheet name
    Set sht1 = ThisWorkbook.Sheets("Sheet1")
    
    ' modify to your desired Pivot Table location
    Set shtReport = ThisWorkbook.Sheets("Report")
    
    ' create the Source Range of the Pivot Cache
    lastRow = sht1.Cells(sht1.Rows.Count, "A").End(xlUp).Row
    
    ' it's looking uo tp Column "O" (15) as recorded in your MACRO
    Set PivotSrc_Range = sht1.Range(sht1.Cells(1, 1), sht1.Cells(lastRow, 15))
    
    ' set the Pivot Cache
    Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotSrc_Range, Version:=xlPivotTableVersion14)
    
    On Error Resume Next
    Set PvtTbl = shtReport.PivotTables("PivotTable1")
    
    On Error GoTo 0
    If PvtTbl Is Nothing Then
        ' create a new Pivot Table in "Report" sheet, start from Cell A2
        Set PvtTbl = shtReport.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=shtReport.Range("A2"), TableName:="PivotTable1")
    
        ' modify the name in brackets according to your Pivot Fields
        With PvtTbl.PivotFields("Customer")
            .Orientation = xlRowField
            .Position = 1
        End With        
    
        PvtTbl.AddDataField PvtTbl.PivotFields("Customer"), "Count of Customer", xlCount
    
    Else
        ' just refresh the Pivot cache with the updated Range (data in Sheet1)
        PvtTbl.ChangePivotCache PvtCache
        PvtTbl.RefreshTable
    End If
    
    ' check if already has a chart in sheet (from previous Macro Runs)
    If shtReport.ChartObjects.Count >= 1 Then
        Set Chart1 = shtReport.ChartObjects(1).Chart
    Else ' first time >> create the chart
        shtReport.Shapes.AddChart.Select
        Set Chart1 = ActiveChart
    End If
    
    With Chart1
        .ChartType = xlColumnClustered
        .SetSourceData Source:=PvtTbl.TableRange1 ' refresh the chart with the updated Pivot Table
    End With
    
    
    End Sub
    

    你能告诉我们这段代码目前做了什么,更具体地说,你现在被困在哪里吗?我想用宏创建Pivot聊天,我录制了宏并尝试运行它。但它不起作用,“不起作用”具体是什么意思?你期望得到什么样的输出?你得到了什么?你得到了错误吗?哪一行?@SivaReddy是否要在数据所在的同一工作表中创建透视表?因为在您的代码中,您正试图用新的PIVOT覆盖数据
    Sub Macro2()
    
    Dim sht1                            As Worksheet
    Dim shtReport                       As Worksheet
    Dim lastRow                         As Long
    Dim PivotSrc_Range                  As Range
    
    Dim PvtCache                        As PivotCache
    Dim PvtTbl                          As PivotTable
    Dim Chart1                          As Chart
    
    ' modify to your sheet name
    Set sht1 = ThisWorkbook.Sheets("Sheet1")
    
    ' modify to your desired Pivot Table location
    Set shtReport = ThisWorkbook.Sheets("Report")
    
    ' create the Source Range of the Pivot Cache
    lastRow = sht1.Cells(sht1.Rows.Count, "A").End(xlUp).Row
    
    ' it's looking uo tp Column "O" (15) as recorded in your MACRO
    Set PivotSrc_Range = sht1.Range(sht1.Cells(1, 1), sht1.Cells(lastRow, 15))
    
    ' set the Pivot Cache
    Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotSrc_Range, Version:=xlPivotTableVersion14)
    
    On Error Resume Next
    Set PvtTbl = shtReport.PivotTables("PivotTable1")
    
    On Error GoTo 0
    If PvtTbl Is Nothing Then
        ' create a new Pivot Table in "Report" sheet, start from Cell A2
        Set PvtTbl = shtReport.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=shtReport.Range("A2"), TableName:="PivotTable1")
    
        ' modify the name in brackets according to your Pivot Fields
        With PvtTbl.PivotFields("Customer")
            .Orientation = xlRowField
            .Position = 1
        End With        
    
        PvtTbl.AddDataField PvtTbl.PivotFields("Customer"), "Count of Customer", xlCount
    
    Else
        ' just refresh the Pivot cache with the updated Range (data in Sheet1)
        PvtTbl.ChangePivotCache PvtCache
        PvtTbl.RefreshTable
    End If
    
    ' check if already has a chart in sheet (from previous Macro Runs)
    If shtReport.ChartObjects.Count >= 1 Then
        Set Chart1 = shtReport.ChartObjects(1).Chart
    Else ' first time >> create the chart
        shtReport.Shapes.AddChart.Select
        Set Chart1 = ActiveChart
    End If
    
    With Chart1
        .ChartType = xlColumnClustered
        .SetSourceData Source:=PvtTbl.TableRange1 ' refresh the chart with the updated Pivot Table
    End With
    
    
    End Sub