+ Reply to Thread
Results 1 to 5 of 5

Dynamic Formating VBA Pivot Table

  1. #1
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182

    Dynamic Formating VBA Pivot Table

    Hello Everyone,

    I am working on a project to FCST sales. Each month we download a spreadsheet, FCST's are done on months, so each month, one month drops off the list and a new one is added. The problem I have, is I recorded a macro to create the pivot table, well when one month drops off, the code errors our because it can't find the hard coded month....how do I fix this. Here is the code to create the macro, it is still pretty raw, but I am sure someone knows how to help:

    Sub FCST ()
    Dim LastRow As Long

    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    ActiveSheet.UsedRange.Select

    Range(Selection, Selection.End(xlDown)).Name = "Pivot_Range"

    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    "=Pivot_Range").CreatePivotTable TableDestination:="", _
    TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select


    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Customer group")
    .Orientation = xlRowField
    .Position = 1
    End With

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Int.Customer Name")
    .Orientation = xlRowField
    .Position = 2
    End With

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Mat.group/Diameter")
    .Orientation = xlRowField
    .Position = 3
    End With

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Final product")
    .Orientation = xlRowField
    .Position = 4
    End With

    Range("D4").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Final product"). _
    Orientation = xlHidden
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Finished Product")
    .Orientation = xlRowField
    .Position = 4
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Nick name")
    .Orientation = xlRowField
    .Position = 5
    End With
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Data2")
    .Orientation = xlPageField
    .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("SAG Fcst. version")
    .Orientation = xlPageField
    .Position = 1
    End With



    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("09.2006"), "Sum of 09.2006", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("10.2006"), "Sum of 10.2006", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("11.2006"), "Sum of 11.2006", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("12.2006"), "Sum of 12.2006", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("01.2007"), "Sum of 01.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("02.2007"), "Sum of 02.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("03.2007"), "Sum of 03.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("04.2007"), "Sum of 04.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("05.2007"), "Sum of 05.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("06.2007"), "Sum of 06.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("07.2007"), "Sum of 07.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("08.2007"), "Sum of 08.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("09.2007"), "Sum of 09.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("10.2007"), "Sum of 10.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("11.2007"), "Sum of 11.2007", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("12.2007"), "Sum of 12.2007", xlSum




    Range("E5").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Nick name").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
    Range("D5").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Finished Product"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
    Range("C5").Select
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Mat.group/Diameter"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
    ActiveWindow.SmallScroll Down:=-9
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Data2").CurrentPage = _
    "FC value (fixed curr.)"
    Range("A1").Select
    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    Range("F5").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of 09.2006")
    .NumberFormat = "#,##0"
    End With
    'With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of ShipQty")
    ' .NumberFormat = "#,##0"

    Range("G5").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of 10.2006")
    .NumberFormat = "#,##0"
    End With

    With ActiveSheet.PivotTables("PivotTable1").DataPivotField
    .Orientation = xlColumnField
    .Position = 1
    End With
    Range("A1").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Data2")
    .PivotItems("FC Allocation").Visible = False
    .PivotItems("FC Potential QTY").Visible = False
    .PivotItems("FC price (orig. curr.)").Visible = False
    .PivotItems("FC quantity").Visible = False
    .PivotItems("FC Shipp. QTY").Visible = False
    End With
    End Sub

    Thanks, MATT

  2. #2
    Valued Forum Contributor Excelenator's Avatar
    Join Date
    07-25-2006
    Location
    Wantagh, NY
    Posts
    333
    I haven't tested this but you might try to do the following:

    Please Login or Register  to view this content.
    ---------------------------------------------------
    ONLY APPLIES TO VBA RESPONSES WHERE APPROPRIATE
    To insert code into the VBE (Visual Basic Editor)
    1. Copy the code.
    2. Open workbook to paste code into.
    3. Right click any worksheet tab, select View Code
    4. VBE (Visual Basic Editor) opens to that sheets object
    5. You may change to another sheets object or the This Workbook object by double clicking it in the Project window
    6. In the blank space below the word "General" paste the copied code.

  3. #3
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182
    Hello Excelenator,

    Thank you for the response!

    This seems to be for a worksheet...but I need code that will execute with the macro. Do you think there is some kind of if/then statement that will evaluate if each of the conditions exist and if it doesn't if won't error out and look for the next one??

    thanks!

    Matt

  4. #4
    Valued Forum Contributor Excelenator's Avatar
    Join Date
    07-25-2006
    Location
    Wantagh, NY
    Posts
    333
    The code that I posted was directly out of the code that you posted for the pivot table.

  5. #5
    Forum Contributor
    Join Date
    05-18-2004
    Location
    Portland, Oregon
    MS-Off Ver
    2016
    Posts
    182
    Hello Excelenator,

    Perhaps I don't know how to use it then, because the directions say to post it in the worksheet, but I need it in the module. Can you post how the full code would look? I tried what you have, but it errors out on me when I introduce the variables.

    How do you do a basic "If this exits, then format, if not, move on?"

    Thanks,
    Matt

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1