ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   syntax in VBA to create a pivot table (https://www.excelbanter.com/excel-programming/430865-syntax-vba-create-pivot-table.html)

Charles L. Snyder

syntax in VBA to create a pivot table
 
Hi

An easy question:

With this code to create a pivot table:

Sub pt()
Dim LastRow As Long
Dim LastColumn As Long

LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
LastColumn = Cells.Find(What:="*", After:=[A1],
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Sheets.Add.Name = "pivot"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDa tabase,
SourceData:= _
"cases-dump!R1C1:R3857C14",
Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="pivot!R3C1", TableName:="PivotTable1",
DefaultVersion _
:=xlPivotTableVersion12
Sheets("pivot").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields ("Procedure
Date")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataFiel d
ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("rvu"), "Sum of rvu", xlSum
Range("A6").Select
Selection.Group Start:=True, End:=True, Periods:=Array(False,
False, False, _
False, True, False, True)
With ActiveSheet.PivotTables("PivotTable1").PivotFields ("Years")
.Orientation = xlColumnField
.Position = 1
End With
End Sub


what is the correct syntax to replace:

SourceData:= _
"cases-dump!R1C1:R3857C14"

with

SourceData:= _
"cases-dump!R1C1:R [LastRowcd ] C [Last Column]"

I copied the code from the macro editor, and altered it a little, but
need the correct R1C1 notation to use the last row and column in the
(variable) source data.

Thanks!

Charles Snyder

Dave Peterson

syntax in VBA to create a pivot table
 

try:

SourceData:= _
"cases-dump!R1C1:R" & lastrow & "C" & lastcolumn, ...

"Charles L. Snyder" wrote:

Hi

An easy question:

With this code to create a pivot table:

Sub pt()
Dim LastRow As Long
Dim LastColumn As Long

LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
LastColumn = Cells.Find(What:="*", After:=[A1],
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Sheets.Add.Name = "pivot"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDa tabase,
SourceData:= _
"cases-dump!R1C1:R3857C14",
Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="pivot!R3C1", TableName:="PivotTable1",
DefaultVersion _
:=xlPivotTableVersion12
Sheets("pivot").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields ("Procedure
Date")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataFiel d
ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("rvu"), "Sum of rvu", xlSum
Range("A6").Select
Selection.Group Start:=True, End:=True, Periods:=Array(False,
False, False, _
False, True, False, True)
With ActiveSheet.PivotTables("PivotTable1").PivotFields ("Years")
.Orientation = xlColumnField
.Position = 1
End With
End Sub

what is the correct syntax to replace:

SourceData:= _
"cases-dump!R1C1:R3857C14"

with

SourceData:= _
"cases-dump!R1C1:R [LastRowcd ] C [Last Column]"

I copied the code from the macro editor, and altered it a little, but
need the correct R1C1 notation to use the last row and column in the
(variable) source data.

Thanks!

Charles Snyder


--

Dave Peterson

ryguy7272

syntax in VBA to create a pivot table
 

Basically, it is like this:
Range("A1").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _
Sheets("Sheet1").Range("A1").CurrentRegion).Create PivotTable _
TableDestination:=Sheets("Sheet2").Range("A1"), TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

If you have a Sheet named 'cases-dump', then I surmise the code would be this:
Sheets("cases-dump").Range("A1").CurrentRegion).

Also, it seems like you are using XL'07, I use XL'03, thus the
xlPivotTableVersion10
Just watch out for that...

Good luck,
Ryan---

--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Charles L. Snyder" wrote:

Hi

An easy question:

With this code to create a pivot table:

Sub pt()
Dim LastRow As Long
Dim LastColumn As Long

LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows,
SearchDirection:=xlPrevious).Row
LastColumn = Cells.Find(What:="*", After:=[A1],
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Sheets.Add.Name = "pivot"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDa tabase,
SourceData:= _
"cases-dump!R1C1:R3857C14",
Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="pivot!R3C1", TableName:="PivotTable1",
DefaultVersion _
:=xlPivotTableVersion12
Sheets("pivot").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields ("Procedure
Date")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataFiel d
ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("rvu"), "Sum of rvu", xlSum
Range("A6").Select
Selection.Group Start:=True, End:=True, Periods:=Array(False,
False, False, _
False, True, False, True)
With ActiveSheet.PivotTables("PivotTable1").PivotFields ("Years")
.Orientation = xlColumnField
.Position = 1
End With
End Sub


what is the correct syntax to replace:

SourceData:= _
"cases-dump!R1C1:R3857C14"

with

SourceData:= _
"cases-dump!R1C1:R [LastRowcd ] C [Last Column]"

I copied the code from the macro editor, and altered it a little, but
need the correct R1C1 notation to use the last row and column in the
(variable) source data.

Thanks!

Charles Snyder



All times are GMT +1. The time now is 01:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com