VBA to select all data on a sheet, varies each time, then create pivot

LauraWork

Board Regular
Joined
Jan 7, 2013
Messages
79
hi all

I recorded a Macro and I'm chaning all the parts to work regardless of how much data I have but I cant get the pivot to work, I just need it to select all the data on the sheet which will always have 19 columns but the row number will vary each time I run the report.

how can I amend this: I have trieds lots of solutions from here and other places and cant get anything to work. I cant preselect as this shows the word 'blank' and I cant have that in the report: I also need to make sure the pivot is on a new sheet.

Heres the snapshot:

Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R2007C17", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion10
Sheets("Sheet1").Select
Cells(3, 1).Select

thanks in advance
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I do this all the time. Here is a bit of code that may help you. You have most of it.

Code:
dim lastRow as Long
dim lastCol as Integer

lastRow = sheets("Data").usedrange.rows.count
lastCol = sheets("Data").usedrange.columns.count
sheets.Add
[COLOR=#333333]ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _[/COLOR]
[COLOR=#333333]"Data!R1C1:R[/COLOR][COLOR=#ff0000]"&lastRow&"[/COLOR][COLOR=#333333]C[/COLOR][COLOR=#ff0000]"&lastCol[/COLOR][COLOR=#333333], Version:=xlPivotTableVersion10).CreatePivotTable _[/COLOR]
[COLOR=#333333]TableDestination:="C3", TableName:="PivotTable1", DefaultVersion _[/COLOR]
[COLOR=#333333]:=xlPivotTableVersion10[/COLOR]
[COLOR=#333333]Sheets("Sheet1").Select[/COLOR]
[COLOR=#333333]Cells(3, 1).Select[/COLOR]
 
Upvote 0
Hi, I have tried your code and I get a compile error and its highlighting the C bit below as the error - any ideas?

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R2007C17", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="&lastRow&"C"&lastCol, TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion10
 
Upvote 0
You changed the wrong field. The idea is to put the lastrow and lastcol in the "SourceData" not the "TableDestination"
 
Upvote 0
Oh yeah sorry #fail!

I have copied the code above exact and it still wont work? any ideas?




Dim lastRow As Long
Dim lastCol As Integer

lastRow = Sheets("Data").UsedRange.Rows.Count
lastCol = Sheets("Data").UsedRange.Columns.Count
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R"&lastRow&"C"&lastCol, Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="C3", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion10
Sheets("Sheet1").Select
Cells(3, 1).Select
 
Upvote 0
is this right?

Dim lastRow As Long
Dim lastCol As Integer
lastRow = Sheets("Data").UsedRange.Rows.Count
lastCol = Sheets("Data").UsedRange.Columns.Count
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R" & lastRow & "C" & lastCol & "", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="C3", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion10
Sheets("Sheet1").Select
Cells(3, 1).Select
 
Upvote 0
That looks right. However, you should not need to do "Sheets("Sheet1").Select" The sheet you just added may have any name. It will still be the active sheet.

What did it say when you tried that?
 
Upvote 0
That looks right. However, you should not need to do "Sheets("Sheet1").Select" The sheet you just added may have any name. It will still be the active sheet.

What did it say when you tried that?


yes that works great thanks - but I am making multiple pivots from multiple data sources so I will need to select the last pivots pasted value data for the next if you see what I mean so I copied the code again and it says I can't use it twice so how can I get around that?
 
Upvote 0
Hi,

I have an issue with below code, which is similar to the one mentioned earlier in this thread. I'm using excel 2010.
Problem is that I keep receiving an error on the bold part of the code. anyone who sees the problem with this code?

Error message = Runtime error 5. Invalid procedure call or argument
code =

Sub Pivot()
'
' Pivot Macro
'

'
Sheets("Overzicht").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select

Dim lastRow As Long
Dim lastCol As Integer
lastRow = Sheets("Filter").UsedRange.Rows.Count
lastCol = Sheets("Filter").UsedRange.Columns.Count

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Filter!R1C1:R" & lastRow & "C" & lastCol, Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Overview!R1C1", TableName:="Departed", DefaultVersion _
:=xlPivotTableVersion14

With ActiveSheet.PivotTables("Departed").PivotFields("Year departed")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("Departed").AddDataField ActiveSheet.PivotTables( _
"Departed").PivotFields("DL"), "Count of DL", xlCount
With ActiveSheet.PivotTables("Departed").PivotFields("Place")
.Orientation = xlRowField
.Position = 1
End With
Range("E2").Select
ActiveSheet.PivotTables("Departed").RowGrand = False
Range("A13").Select
ActiveSheet.PivotTables("Departed").ColumnGrand = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top