Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 542
Default VBA Pivot Table help with data fields

I have this code, that I found somewhere online and I have adjusted it for my
workbook, but I cannot seem to set up the data fields correctly.

The macro takes a report of usage of a particular application and sums up
the hours that person has been using it. Than it creates a pivot table with
that data. That is where I am stuck. I can get the Pivot Table to create
but I cannot seem to add an additional data filed.

Here is what I have in pivot table:
A1 = Count of Elapsed Time
A2 = User name
B2 = Total (numbers of hours spent in the application

I want to add another data field called "Hour", so that it adds it in the
cell B1.
Below is the code I have, it works fine I just need it to add that other
data field "Hour"
Sub format()

Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim pt As PivotTable
Dim strField As String
Dim WSD As Worksheet
Set WSD = Worksheets("base data")
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("Sheet2")
Dim PTCache As PivotCache
Dim PRange As Range

'Insert Column headers into base data worksheet
Range("G1").Select
ActiveCell.FormulaR1C1 = "Hour"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Day"

'Insert Formula for Hour and Day calculations
Range("G2").Select
ActiveCell.FormulaR1C1 = "=HOUR(RC[-6])"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=DAY(RC[-7])"
Range("H3").Select

'Formula was in G2 through N2, wanted to copy down G2 to column N
Range("G2:N2").AutoFill Destination:=Range("G2:N" & LR)

' Find the last row with data
Dim finalRow As Long
finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row

' Find the last column with data
Dim finalCol As Long
finalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column

' Find the range of the data
Set PRange = WSD.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:=PRange)

' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutpu t.Cells(1, 1), _
TableName:="SamplePivot")

' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying out

pt.ManualUpdate = True

' Set up the row fields

pt.AddFields RowFields:=Array("User name")
'pt.AddFields ColumnFields:=Array("Hour")

' Set up the data fields
With pt.PivotFields("Elapsed time")
.Orientation = xlDataField
.Function = xlCount
.Position = 1

End With

' Now calc the pivot table
pt.ManualUpdate = False

End Sub

Thanks for the help

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default VBA Pivot Table help with data fields

When I'm creating this kind of thing, I'll record a macro when I do it
manually.

With all the options that you could be choosing from, I'm not sure anyone could
guess what you really want.

If you have trouble merging the recorded code into your existing code, post back
with that recorded code (and the current code if you changed it).

James wrote:

I have this code, that I found somewhere online and I have adjusted it for my
workbook, but I cannot seem to set up the data fields correctly.

The macro takes a report of usage of a particular application and sums up
the hours that person has been using it. Than it creates a pivot table with
that data. That is where I am stuck. I can get the Pivot Table to create
but I cannot seem to add an additional data filed.

Here is what I have in pivot table:
A1 = Count of Elapsed Time
A2 = User name
B2 = Total (numbers of hours spent in the application

I want to add another data field called "Hour", so that it adds it in the
cell B1.
Below is the code I have, it works fine I just need it to add that other
data field "Hour"
Sub format()

Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim pt As PivotTable
Dim strField As String
Dim WSD As Worksheet
Set WSD = Worksheets("base data")
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("Sheet2")
Dim PTCache As PivotCache
Dim PRange As Range

'Insert Column headers into base data worksheet
Range("G1").Select
ActiveCell.FormulaR1C1 = "Hour"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Day"

'Insert Formula for Hour and Day calculations
Range("G2").Select
ActiveCell.FormulaR1C1 = "=HOUR(RC[-6])"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=DAY(RC[-7])"
Range("H3").Select

'Formula was in G2 through N2, wanted to copy down G2 to column N
Range("G2:N2").AutoFill Destination:=Range("G2:N" & LR)

' Find the last row with data
Dim finalRow As Long
finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row

' Find the last column with data
Dim finalCol As Long
finalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column

' Find the range of the data
Set PRange = WSD.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:=PRange)

' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutpu t.Cells(1, 1), _
TableName:="SamplePivot")

' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying out

pt.ManualUpdate = True

' Set up the row fields

pt.AddFields RowFields:=Array("User name")
'pt.AddFields ColumnFields:=Array("Hour")

' Set up the data fields
With pt.PivotFields("Elapsed time")
.Orientation = xlDataField
.Function = xlCount
.Position = 1

End With

' Now calc the pivot table
pt.ManualUpdate = False

End Sub

Thanks for the help


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 542
Default VBA Pivot Table help with data fields

Here is the revised code with what I came up with (didn't work), and what I
recorded. What I recorded worked in a seperate module but it did not work
with my original code. I know it must be something small I am forgetting.

The new code is at the bottom. What I am trying to do is to add a column
field not a data field. I am not very good with these tables, and their
properties.

Sub format()

Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim pt As PivotTable
Dim strField As String
Dim WSD As Worksheet
Set WSD = Worksheets("base data")
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("Sheet2")
Dim PTCache As PivotCache
Dim PRange As Range

'Insert Column headers into base data worksheet
Range("G1").Select
ActiveCell.FormulaR1C1 = "Hour"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Day"

'Insert Formula for Hour and Day calculations
Range("G2").Select
ActiveCell.FormulaR1C1 = "=HOUR(RC[-6])"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=DAY(RC[-7])"
Range("H3").Select

'Formula was in G2 through N2, wanted to copy down G2 to column N
Range("G2:N2").AutoFill Destination:=Range("G2:N" & LR)

' Find the last row with data
Dim finalRow As Long
finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row

' Find the last column with data
Dim finalCol As Long
finalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column

' Find the range of the data
Set PRange = WSD.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:=PRange)

' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutpu t.Cells(1, 1), _
TableName:="SamplePivot")

' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying out

pt.ManualUpdate = True

' Set up the row fields

pt.AddFields RowFields:=Array("User name")
'pt.AddFields ColumnFields:=Array("Hour")

' Set up the data fields
With pt.PivotFields("Elapsed time")
.Orientation = xlDataField
.Function = xlCount
.Position = 1

'This is what I originally came up with, but didn't work
'With pt.PivotFields("Hour")
' .Orientation = xlColumnField
' .Position = 1

'This is what I recorded
' With ActiveSheet.PivotTables("SamplePivot").PivotFields ("Hour")
' .Orientation = xlColumnField
' .Position = 1



End With

' Now calc the pivot table
pt.ManualUpdate = False

End Sub

"Dave Peterson" wrote:

When I'm creating this kind of thing, I'll record a macro when I do it
manually.

With all the options that you could be choosing from, I'm not sure anyone could
guess what you really want.

If you have trouble merging the recorded code into your existing code, post back
with that recorded code (and the current code if you changed it).

James wrote:

I have this code, that I found somewhere online and I have adjusted it for my
workbook, but I cannot seem to set up the data fields correctly.

The macro takes a report of usage of a particular application and sums up
the hours that person has been using it. Than it creates a pivot table with
that data. That is where I am stuck. I can get the Pivot Table to create
but I cannot seem to add an additional data filed.

Here is what I have in pivot table:
A1 = Count of Elapsed Time
A2 = User name
B2 = Total (numbers of hours spent in the application

I want to add another data field called "Hour", so that it adds it in the
cell B1.
Below is the code I have, it works fine I just need it to add that other
data field "Hour"
Sub format()

Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim pt As PivotTable
Dim strField As String
Dim WSD As Worksheet
Set WSD = Worksheets("base data")
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("Sheet2")
Dim PTCache As PivotCache
Dim PRange As Range

'Insert Column headers into base data worksheet
Range("G1").Select
ActiveCell.FormulaR1C1 = "Hour"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Day"

'Insert Formula for Hour and Day calculations
Range("G2").Select
ActiveCell.FormulaR1C1 = "=HOUR(RC[-6])"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=DAY(RC[-7])"
Range("H3").Select

'Formula was in G2 through N2, wanted to copy down G2 to column N
Range("G2:N2").AutoFill Destination:=Range("G2:N" & LR)

' Find the last row with data
Dim finalRow As Long
finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row

' Find the last column with data
Dim finalCol As Long
finalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column

' Find the range of the data
Set PRange = WSD.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:=PRange)

' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutpu t.Cells(1, 1), _
TableName:="SamplePivot")

' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying out

pt.ManualUpdate = True

' Set up the row fields

pt.AddFields RowFields:=Array("User name")
'pt.AddFields ColumnFields:=Array("Hour")

' Set up the data fields
With pt.PivotFields("Elapsed time")
.Orientation = xlDataField
.Function = xlCount
.Position = 1

End With

' Now calc the pivot table
pt.ManualUpdate = False

End Sub

Thanks for the help


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 542
Default VBA Pivot Table help with data fields

Dave,
I figured out the problem. I was missing the "End With", it works now I
don't know how I missed that one.

"Dave Peterson" wrote:

When I'm creating this kind of thing, I'll record a macro when I do it
manually.

With all the options that you could be choosing from, I'm not sure anyone could
guess what you really want.

If you have trouble merging the recorded code into your existing code, post back
with that recorded code (and the current code if you changed it).

James wrote:

I have this code, that I found somewhere online and I have adjusted it for my
workbook, but I cannot seem to set up the data fields correctly.

The macro takes a report of usage of a particular application and sums up
the hours that person has been using it. Than it creates a pivot table with
that data. That is where I am stuck. I can get the Pivot Table to create
but I cannot seem to add an additional data filed.

Here is what I have in pivot table:
A1 = Count of Elapsed Time
A2 = User name
B2 = Total (numbers of hours spent in the application

I want to add another data field called "Hour", so that it adds it in the
cell B1.
Below is the code I have, it works fine I just need it to add that other
data field "Hour"
Sub format()

Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim pt As PivotTable
Dim strField As String
Dim WSD As Worksheet
Set WSD = Worksheets("base data")
Dim PTOutput As Worksheet
Set PTOutput = Worksheets("Sheet2")
Dim PTCache As PivotCache
Dim PRange As Range

'Insert Column headers into base data worksheet
Range("G1").Select
ActiveCell.FormulaR1C1 = "Hour"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Day"

'Insert Formula for Hour and Day calculations
Range("G2").Select
ActiveCell.FormulaR1C1 = "=HOUR(RC[-6])"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=DAY(RC[-7])"
Range("H3").Select

'Formula was in G2 through N2, wanted to copy down G2 to column N
Range("G2:N2").AutoFill Destination:=Range("G2:N" & LR)

' Find the last row with data
Dim finalRow As Long
finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row

' Find the last column with data
Dim finalCol As Long
finalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column

' Find the range of the data
Set PRange = WSD.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:=PRange)

' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutpu t.Cells(1, 1), _
TableName:="SamplePivot")

' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying out

pt.ManualUpdate = True

' Set up the row fields

pt.AddFields RowFields:=Array("User name")
'pt.AddFields ColumnFields:=Array("Hour")

' Set up the data fields
With pt.PivotFields("Elapsed time")
.Orientation = xlDataField
.Function = xlCount
.Position = 1

End With

' Now calc the pivot table
pt.ManualUpdate = False

End Sub

Thanks for the help


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Fields in Pivot Table MichaelR Excel Discussion (Misc queries) 0 July 22nd 08 06:36 PM
data as a percetage of row fields in pivot table Eqa Excel Discussion (Misc queries) 0 May 7th 08 06:44 AM
Pivot Table Macro not inserting all data fields [email protected] Excel Discussion (Misc queries) 0 October 27th 06 04:55 AM
how do i create pivot table from data fields containing forumulas Pam Deshazier, SRHS Excel Worksheet Functions 1 June 21st 06 11:03 PM
Manipulating Pivot Table Data Fields Jay Charts and Charting in Excel 1 November 1st 05 03:48 AM


All times are GMT +1. The time now is 05:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"