Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Add all fields to pivot using VBA

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 468
Default Add all fields to pivot using VBA

Have you tried Recording a Macro to see what it provides you with?
Macro Recorder is your friend...

"JimmyA" wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Add all fields to pivot using VBA

The number of columns from C onwards can be variable so recording isn't
appropriate

"JMay" wrote:

Have you tried Recording a Macro to see what it provides you with?
Macro Recorder is your friend...

"JimmyA" wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default Add all fields to pivot using VBA

Jmay's suggestion actually is a good one. Even if you can't record the exact
final result you want, you can use the recorder to see the correctly coded
syntax for talking to a pivot table through VBA code. Then modify with some
variables and looping to account for how many columns need to be added, and
you're essentially there. Added bonus of being a great way to build your own
VB skills! :)


"JimmyA" wrote:

The number of columns from C onwards can be variable so recording isn't
appropriate

"JMay" wrote:

Have you tried Recording a Macro to see what it provides you with?
Macro Recorder is your friend...

"JimmyA" wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Add all fields to pivot using VBA

Slarbie/JMay, got a tight deadline on this. I use loops from time to time but
not sure how to pick up the field and set as variable then loop till last
column (within the pivot VB code) . There can be anything from 60 -80
columns. have you got any example loops to pick up the header, add as data
field then move to next column until completed?

Thanks for the quick responses guys :-)

"slarbie" wrote:

Jmay's suggestion actually is a good one. Even if you can't record the exact
final result you want, you can use the recorder to see the correctly coded
syntax for talking to a pivot table through VBA code. Then modify with some
variables and looping to account for how many columns need to be added, and
you're essentially there. Added bonus of being a great way to build your own
VB skills! :)


"JimmyA" wrote:

The number of columns from C onwards can be variable so recording isn't
appropriate

"JMay" wrote:

Have you tried Recording a Macro to see what it provides you with?
Macro Recorder is your friend...

"JimmyA" wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Add all fields to pivot using VBA

I used column A to determine the number of rows that should be included in the
pivottable range. And I used Row 1 to determine the number of columns.

I assume that the headers are all nicely valid--no duplicates and no blanks,
right. And that you want Sum for each of those other fields.

Option Explicit
Sub testme()

Dim myRng As Range
Dim LastRow As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim iCol As Long
Dim RowFieldArray() As String
Dim DataFieldArray() As String
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
.Activate
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 3 'skipping columns A and B
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

Set myRng = .Range("A1", .Cells(LastRow, LastCol))

ReDim RowFieldArray(1 To 3)
RowFieldArray(1) = .Range("a1").Value
RowFieldArray(2) = .Range("b1").Value
RowFieldArray(3) = "Data"

ReDim DataFieldArray(3 To LastCol)
For iCol = 3 To LastCol
DataFieldArray(iCol) = .Cells(1, iCol).Value
Next iCol

.Parent.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=myRng.Address(external:=True)).CreateP ivotTable _
TableDestination:="", _
TableName:="PT" & Format(Now, "yyyymmdd_hhmmss"), _
DefaultVersion:=xlPivotTableVersion10
End With

With ActiveSheet
.PivotTableWizard TableDestination:=.Range("A3")

.PivotTables(1).AddFields RowFields:=RowFieldArray

For iCol = 3 To LastCol
With .PivotTables(1).PivotFields(iCol)
.Orientation = xlDataField
.Position = iCol - 2
.Function = xlSum
End With
Next iCol

With .PivotTables(1).DataPivotField
.Orientation = xlColumnField
.Position = 1
End With

.UsedRange.Columns.AutoFit
.Range("a1").Select 'just to make sure it's visible
.Range("C5").Select
ActiveWindow.FreezePanes = True
End With
End Sub

It seemed to work ok for me in xl2003.

JimmyA wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Add all fields to pivot using VBA

I used column A to determine the number of rows that should be included in the
pivottable range. And I used Row 1 to determine the number of columns.

I assume that the headers are all nicely valid--no duplicates and no blanks,
right. And that you want Sum for each of those other fields.

Option Explicit
Sub testme()

Dim myRng As Range
Dim LastRow As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim iCol As Long
Dim RowFieldArray() As String
Dim DataFieldArray() As String
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
.Activate
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 3 'skipping columns A and B
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

Set myRng = .Range("A1", .Cells(LastRow, LastCol))

ReDim RowFieldArray(1 To 3)
RowFieldArray(1) = .Range("a1").Value
RowFieldArray(2) = .Range("b1").Value
RowFieldArray(3) = "Data"

ReDim DataFieldArray(3 To LastCol)
For iCol = 3 To LastCol
DataFieldArray(iCol) = .Cells(1, iCol).Value
Next iCol

.Parent.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=myRng.Address(external:=True)).CreateP ivotTable _
TableDestination:="", _
TableName:="PT" & Format(Now, "yyyymmdd_hhmmss"), _
DefaultVersion:=xlPivotTableVersion10
End With

With ActiveSheet
.PivotTableWizard TableDestination:=.Range("A3")

.PivotTables(1).AddFields RowFields:=RowFieldArray

For iCol = 3 To LastCol
With .PivotTables(1).PivotFields(iCol)
.Orientation = xlDataField
.Position = iCol - 2
.Function = xlSum
End With
Next iCol

With .PivotTables(1).DataPivotField
.Orientation = xlColumnField
.Position = 1
End With

.UsedRange.Columns.AutoFit
.Range("a1").Select 'just to make sure it's visible
.Range("C5").Select
ActiveWindow.FreezePanes = True
End With
End Sub

It seemed to work ok for me in xl2003.

JimmyA wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Add all fields to pivot using VBA

Thanks Dave, this is exactly what I need. You rule

"Dave Peterson" wrote:

I used column A to determine the number of rows that should be included in the
pivottable range. And I used Row 1 to determine the number of columns.

I assume that the headers are all nicely valid--no duplicates and no blanks,
right. And that you want Sum for each of those other fields.

Option Explicit
Sub testme()

Dim myRng As Range
Dim LastRow As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim iCol As Long
Dim RowFieldArray() As String
Dim DataFieldArray() As String
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
.Activate
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 3 'skipping columns A and B
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

Set myRng = .Range("A1", .Cells(LastRow, LastCol))

ReDim RowFieldArray(1 To 3)
RowFieldArray(1) = .Range("a1").Value
RowFieldArray(2) = .Range("b1").Value
RowFieldArray(3) = "Data"

ReDim DataFieldArray(3 To LastCol)
For iCol = 3 To LastCol
DataFieldArray(iCol) = .Cells(1, iCol).Value
Next iCol

.Parent.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=myRng.Address(external:=True)).CreateP ivotTable _
TableDestination:="", _
TableName:="PT" & Format(Now, "yyyymmdd_hhmmss"), _
DefaultVersion:=xlPivotTableVersion10
End With

With ActiveSheet
.PivotTableWizard TableDestination:=.Range("A3")

.PivotTables(1).AddFields RowFields:=RowFieldArray

For iCol = 3 To LastCol
With .PivotTables(1).PivotFields(iCol)
.Orientation = xlDataField
.Position = iCol - 2
.Function = xlSum
End With
Next iCol

With .PivotTables(1).DataPivotField
.Orientation = xlColumnField
.Position = 1
End With

.UsedRange.Columns.AutoFit
.Range("a1").Select 'just to make sure it's visible
.Range("C5").Select
ActiveWindow.FreezePanes = True
End With
End Sub

It seemed to work ok for me in xl2003.

JimmyA wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...


--

Dave Peterson

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Add all fields to pivot using VBA

Thanks Dave, this is exactly what I need. You rule

"Dave Peterson" wrote:

I used column A to determine the number of rows that should be included in the
pivottable range. And I used Row 1 to determine the number of columns.

I assume that the headers are all nicely valid--no duplicates and no blanks,
right. And that you want Sum for each of those other fields.

Option Explicit
Sub testme()

Dim myRng As Range
Dim LastRow As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim iCol As Long
Dim RowFieldArray() As String
Dim DataFieldArray() As String
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
.Activate
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 3 'skipping columns A and B
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

Set myRng = .Range("A1", .Cells(LastRow, LastCol))

ReDim RowFieldArray(1 To 3)
RowFieldArray(1) = .Range("a1").Value
RowFieldArray(2) = .Range("b1").Value
RowFieldArray(3) = "Data"

ReDim DataFieldArray(3 To LastCol)
For iCol = 3 To LastCol
DataFieldArray(iCol) = .Cells(1, iCol).Value
Next iCol

.Parent.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=myRng.Address(external:=True)).CreateP ivotTable _
TableDestination:="", _
TableName:="PT" & Format(Now, "yyyymmdd_hhmmss"), _
DefaultVersion:=xlPivotTableVersion10
End With

With ActiveSheet
.PivotTableWizard TableDestination:=.Range("A3")

.PivotTables(1).AddFields RowFields:=RowFieldArray

For iCol = 3 To LastCol
With .PivotTables(1).PivotFields(iCol)
.Orientation = xlDataField
.Position = iCol - 2
.Function = xlSum
End With
Next iCol

With .PivotTables(1).DataPivotField
.Orientation = xlColumnField
.Position = 1
End With

.UsedRange.Columns.AutoFit
.Range("a1").Select 'just to make sure it's visible
.Range("C5").Select
ActiveWindow.FreezePanes = True
End With
End Sub

It seemed to work ok for me in xl2003.

JimmyA wrote:

Hi Guys

I need a macro to create a pivot table for multiple columns. I need column A
and B as Row Fields. Colums C to column x (variable) need to be added
automatically as Pivot fields. The headers for column C to x will be variable.

thanks in advance...


--

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
Problem inserting calculated pivot fields into Pivot Table (2007) wamiller36 Excel Discussion (Misc queries) 1 March 5th 10 11:40 PM
fields names do not show in excel 2007 pivot table fields list marlo17 New Users to Excel 2 December 1st 08 01:25 PM
Pivot Data fields - removal of all (value) fields Bradley Searle[_2_] Excel Programming 2 November 13th 08 02:09 PM
Macro on filtering pivot table (pivot fields) = debug markx Excel Programming 2 May 28th 08 09:32 AM
number of fields in the row fields in pivot table UT Excel Discussion (Misc queries) 0 April 13th 06 01:17 AM


All times are GMT +1. The time now is 06:45 PM.

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

About Us

"It's about Microsoft Excel"