Sub 피벗생성()

    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "sample!R1C1:R200C6", Version:=7).CreatePivotTable TableDestination:= _
        "Sheet1!R3C1", TableName:="피벗 테이블1", DefaultVersion:=7
    Sheets("Sheet1").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("피벗 테이블1")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With
    With ActiveSheet.PivotTables("피벗 테이블1").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("피벗 테이블1").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("피벗 테이블1").PivotFields("지역")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("피벗 테이블1").PivotFields("지점")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("피벗 테이블1").PivotFields("월")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("피벗 테이블1").AddDataField ActiveSheet.PivotTables( _
        "피벗 테이블1").PivotFields("판매액"), "합계 : 판매액", xlSum
    Range("A9").Select
    ActiveSheet.PivotTables("피벗 테이블1").PivotFields("지역").PivotItems("서울").Position _
        = 1
    Range("A3").Select
    With ActiveSheet.PivotTables("피벗 테이블1").PivotFields("합계 : 판매액")
        .Caption = "매출"
        .NumberFormat = "#,##0_ "
    End With
End Sub