+ Reply to Thread
Results 1 to 9 of 9

Thread: simple steps for creating a pivot table using VBA?

  1. #1
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    simple steps for creating a pivot table using VBA?

    Hi,

    I have pretty stanard timestamped data in one sheet and I'd like to create a Pivot table from it in another sheet.

    I'd hoped that VBA would just record the steps I was taking in Excel, but this doesn't seem to happen.

    Could someone take me through creating a Pivot table using VBA?

  2. #2
    Valued Forum Contributor pr4t3ek's Avatar
    Join Date
    10-13-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2003 & 2007
    Posts
    471

    Re: simple steps for creating a pivot table using VBA?

    paste your data and i will then provide you the code
    --
    Regards
    PD

    ----- Don't Forget -----

    1. Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks

    2. Thank those who have helped you by Clicking the scales above each post.

    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  3. #3
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: simple steps for creating a pivot table using VBA?

    Ok,

    I have attached the data.

    You'll see the two pivot tables I have created from the data on two separate sheets. I'd like to make these using VBA.

    Also, as the time is in hours I'd like to group hours 24 and 0 as these are the same.

    I can record all the steps I take until the pivots which is great. I have a macro to extract data from the pivots into a table. This is approximately that macro, though with diffferent references:

    Sub Example2()
    With Sheets("24 hour data BI")
    With .Range(.Cells(2, "A"), Cells(.Rows.Count, "A").End(xlUp)).Offset(, 1).Resize(, 24)
    .FormulaR1C1 = "=IF(ISERROR(GETPIVOTDATA(""time round2"",Sheet4!R1C1, ""Station"",RC1, ""time round2"",R1C)),0,GETPIVOTDATA(""time round2"",Sheet4!R1C1, ""Station"",RC1, ""time round2"",R1C))"
    End With
    End With
    End Sub


    Once the data is in the table - hours along the top and stations down the left I want to make graphs for each station. this might be another task..

    Anyway any help you could lend me either on the pivots or the graphs would be awesome.
    Attached Files Attached Files

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: simple steps for creating a pivot table using VBA?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  5. #5
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: simple steps for creating a pivot table using VBA?

    ok, I'll post better next time.

  6. #6
    Valued Forum Contributor pr4t3ek's Avatar
    Join Date
    10-13-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2003 & 2007
    Posts
    471

    Re: simple steps for creating a pivot table using VBA?

    Hi there,

    You're working for MelbourneBikeShare? Nice! I haven't used the bikes yet but a pretty nice idea.

    code to create the BI Pivot:-
    Sub BI_PT()
        Sheets("Adj Data BI").Select
        ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
            "'Adj Data BI'!A:H").CreatePivotTable TableDestination:="", TableName:= _
            "PT_BI", DefaultVersion:=xlPivotTableVersion10
        ActiveSheet.PivotTables("PT_BI").AddFields RowFields:="Station", ColumnFields:="time"
        ActiveSheet.PivotTables("PT_BI").PivotFields("t").Orientation = xlDataField
        ActiveSheet.PivotTables("PT_BI").PivotFields("time").AutoSort xlAscending, "time"
        ActiveSheet.PivotTables("PT_BI").PivotFields("Station").AutoSort xlAscending, "Station"
        ActiveSheet.PivotTables("PT_BI").PivotSelect "", xlDataAndLabel, True
    End Sub
    Code to create the BO Pivot:-
    Sub BO_PT()
        Sheets("Adj Data BO").Select
        ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
            "'Adj Data BO'!A:H").CreatePivotTable TableDestination:="", TableName:= _
            "PT_BO", DefaultVersion:=xlPivotTableVersion10
        ActiveSheet.PivotTables("PT_BO").AddFields RowFields:="Station", ColumnFields:="time"
        ActiveSheet.PivotTables("PT_BO").PivotFields("t").Orientation = xlDataField
        ActiveSheet.PivotTables("PT_BO").PivotFields("time").AutoSort xlAscending, "time"
        ActiveSheet.PivotTables("PT_BO").PivotFields("Station").AutoSort xlAscending, "Station"
        ActiveSheet.PivotTables("PT_BO").PivotSelect "", xlDataAndLabel, True
    End Sub
    Let me know if there are any issues.

    Cheers
    --
    Regards
    PD

    ----- Don't Forget -----

    1. Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks

    2. Thank those who have helped you by Clicking the scales above each post.

    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  7. #7
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: simple steps for creating a pivot table using VBA?

    Thanks for the code!

    It is great I have been able to move leaps ahead.

    you are right this is data from the MBS. Hopefully I get get something useful out of it.

    I have another 3 (4) requests for assistance on this data.

    1. In the time row at the top of the pivot table I'd like it to show all the hours of the day, regardless of whether or not someting happened at theat time. for large amounts of data all time periods will be shown but if, say, nothing happened at 5am then this would be skipped. I will be graphing this data so need 24 equal intervals.
    2. I'd like to group the 0 and 24 hour data as it is in the same interval. I'd like to label this as '0'.
    3. I'd like to group the days of the week into 'weekdays' and 'weekends'
    4 And this might have to be a new post, I'd like to create graphs for each of the stations on each of 'weekdays' and 'weekends' ie 100 graphs. For this I just don't know at all where to start.

    Help greatly appreciated,

    HW
    apologies if this is duplicated, I had some trouble posting..

  8. #8
    Valued Forum Contributor pr4t3ek's Avatar
    Join Date
    10-13-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2003 & 2007
    Posts
    471

    Re: simple steps for creating a pivot table using VBA?

    1. In the time row at the top of the pivot table I'd like it to show all the hours of the day, regardless of whether or not someting happened at theat time. for large amounts of data all time periods will be shown but if, say, nothing happened at 5am then this would be skipped. I will be graphing this data so need 24 equal intervals.
    In your 'Adj Data BI' and 'Adj Data BO' add numbers 1 to 24 in column G. You do not need to put anything in these any of the other columns for these 1-24 rows.

    Add
    ActiveSheet.PivotTables("PT_BI").PivotFields("time").ShowAllItems = True
    to the BI code.

    Add
    ActiveSheet.PivotTables("PT_BO").PivotFields("time").ShowAllItems = True
    to the BO code.

    2. I'd like to group the 0 and 24 hour data as it is in the same interval. I'd like to label this as '0'.
    You will have to perform an IF formula in your Data sheets for this.

    3. I'd like to group the days of the week into 'weekdays' and 'weekends'
    You would have to use the weekday and if formula's for this.

    4 And this might have to be a new post, I'd like to create graphs for each of the stations on each of 'weekdays' and 'weekends' ie 100 graphs. For this I just don't know at all where to start.
    Really? anyway yeah, after you have done the above 3, paste this as another thread.

    I can do this for you but it's probably best for you to try these yourself, especially because these requests are relatively simple. Let me know otherwise
    --
    Regards
    PD

    ----- Don't Forget -----

    1. Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks

    2. Thank those who have helped you by Clicking the scales above each post.

    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  9. #9
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Thumbs up Re: simple steps for creating a pivot table using VBA?

    done, thanks..

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0