检查自己的网络情况
Sub 数据分析()
Dim ws As Worksheet
Set ws = ActiveSheet ' 获取当前活动 sheet
Dim analyse_ws As Worksheet
On Error Resume Next ' 尝试打开 "analyse" sheet ,如果不存在则出错,然后创建一个新的 sheet
Set analyse_ws = ThisWorkbook.Worksheets("analyse")
If analyse_ws Is Nothing Then
Set analyse_ws = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
analyse_ws.Name = "analyse"
End If
On Error GoTo 0
' 数据透视表设置
analyse_ws.Cells.ClearContents
analyse_ws.Activate
Dim pvtTable As PivotTable
Set pvtTable = analyse_ws.PivotTableWizard(SourceType:=xlDatabase, SourceData:=ws.UsedRange, TableDestination:=analyse_ws.Range("A1"), TableName:="数据透视表")
With pvtTable
.PivotFields("E").Orientation = xlRowField
.AddDataField .PivotFields("F"), "F 求和", xlSum
.AddDataField .PivotFields("G"), "G 求和", xlSum
End With
End Sub