带有宏的Excel数据透视表

时间:2017-04-20 14:18:52

标签: excel vba excel-vba pivot-table

我录制了一个生成非常简单的数据透视表的宏。当我回放宏时,我在PivotTable中收到错误。

我明白了:

  

无效的过程调用或参数

所以我回去并在SourceDataTableDestination周围加上单引号。现在我得到一个数据透视表,但只有总数。它应该给我计算A列中所有项目的出现次数。

这是代码

 Sub testpivot()
'
' testpivot Macro
'
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "'GF Response Detail R'!R1C1:R65536C1", Version:= _
    xlPivotTableVersion10).CreatePivotTable TableDestination:= _
    "'GF Response Detail R'!R2C10", TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion10
Sheets("GF Response Detail R").Select
Cells(2, 7).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Region")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Region"), "Count of Region", xlCount
    ActiveWorkbook.ShowPivotTableFieldList = False

End Sub

3 个答案:

答案 0 :(得分:2)

您想要从工作表中删除数据透视表。在运行宏之前。

答案 1 :(得分:1)

下面的代码将首先检查是否有" PivotTable1"在工作表中,如果它,它将删除它。

然后,它将在&#34; GF响应明细R&#34;上创建一个新的Option Explicit Sub testpivot() ' testpivot Macro Dim PivTbl As PivotTable Dim PivCache As PivotCache Dim DataSht As Worksheet Dim lastRow As Long Dim SrcRng As Range Dim SrcData As String ' set the Pivot Data Set DataSht = Worksheets("GF Response Detail R") With DataSht lastRow = .Range("A" & .Rows.Count).End(xlUp).Row '<-- get last row in Column A Set SrcRng = .Range("A1:A" & lastRow) '<-- set dynamic Pivot Range SrcData = SrcRng.Address(True, True, xlA1, xlExternal) '<-- get the Range Address, including sheet's name End With '-- first check if there's a "PivotTable1" in sheet >> if Yes, Delete it For Each PivTbl In DataSht.PivotTables If PivTbl.Name = "PivotTable1" Then DataSht.Range(PivTbl.TableRange2.Address).Delete Shift:=xlUp Exit For End If Next PivTbl ' set the Pivot Cache 'Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData) ' Option 2: set the Pivot Cache Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcRng) ' Option 3: set the Pivot Cache Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData, Version:=xlPivotTableVersion15) ' create a new Pivot Table in "EXP Pivot" sheet, start from Cell A1 Set PivTbl = DataSht.PivotTables.Add(PivotCache:=PivCache, TableDestination:=DataSht.Range("J2"), TableName:="PivotTable1") With PivTbl With .PivotFields("Region") .Orientation = xlRowField .Position = 1 End With .AddDataField .PivotFields("Region"), "Count of Region", xlCount ActiveWorkbook.ShowPivotTableFieldList = False End With End Sub 。表格,列中的更新数据&#34; A&#34;。

<强>代码

library(shiny)

function(input, output) {

  # upload the csv file

  output$contents <- renderTable({

    inputFile <- input$file1

    if (is.null(inputFile))
      return(NULL)

    read.csv(inputFile$datapath, header=input$header, sep=',',
             quote="'")
  })

  # Create a frame with our values

  sliderValues <- reactive({

    # Compose data frame
    data.frame(
      Name = c("cost", 
               "range"),
               Value = as.character(c(input$cost, 
                                      paste(input$range, collapse=' ')
               ), 
               stringsAsFactors=FALSE)
    )
  }) 

  # Show the values
  output$values <- renderTable({
    sliderValues()
  })
}

答案 2 :(得分:0)

在这里尝试一下。这与@Shai Rado的建议略有不同。

Sub RegionMacro()

Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long

'define the sheet, last row, and pivot table range
Set DSheet = Worksheets("GF Response Detail R")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
Set PRange = DSheet.Range("A1:A" & LastRow)

'get address of range
sData = PRange.Address(False, True)

'check to see if the table already exist and delete it if it does
For Each PTable In DSheet.PivotTables
    If PTable.Name = "RegionCountTable" Then
        DSheet.Range(PTable.TableRange2.Address).Delete Shift:=xlUp
        Exit For
    End If
Next PTable

'define the pivot cache
Set PCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)

'insert a blank pivot table into cell G2 and call it "RegionCountTable"
Set PTable = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(2, 7), TableName:="RegionCountTable")

'insert row fields
With PTable

    With ActiveSheet.PivotTables("RegionCountTable").PivotFields("Region")
     .Orientation = xlRowField
     .Position = 1
    End With

    'create a count for the region
    .AddDataField .PivotFields("Region"), "Count of Region", xlCount
    ActiveWorkbook.ShowPivotTableFieldList = False
End With


End Sub