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?
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.
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.
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)
ok, I'll post better next time.
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:-
Code to create the BO 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
Let me know if there are any issues.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
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.
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..
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.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.
Addto the BI code.ActiveSheet.PivotTables("PT_BI").PivotFields("time").ShowAllItems = True
Addto the BO code.ActiveSheet.PivotTables("PT_BO").PivotFields("time").ShowAllItems = True
You will have to perform an IF formula in your Data sheets for this.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 would have to use the weekday and if formula's for this.3. I'd like to group the days of the week into 'weekdays' and 'weekends'
Really? anyway yeah, after you have done the above 3, paste this as another thread.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.
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.
done, thanks..
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks