Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pivot Table source data reference in VBA

Status
Not open for further replies.

heavnstrl

Technical User
Mar 2, 2003
4
US
I am creating a pivot table in which the data source (on a sheet named "results") has a variable number of rows. I can hard code the number of rows but would prefer to just refer to the 'used range' instead. The statement calling the pivot table currently looks like this.

ActiveWorkbook.PivotCaches.add(SourceType:=xlDatabase, SourceData:= _
"Results!R1C1:R3000C36").CreatePivotTable TableDestination:="", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10

Whenever I substitute a variable referencing the used range I get a compiler error "Expected list separater or )". Does anyone have any suggestions?
 
Figured out my own question. Adding code as follows fixed the problem:

prows = resultsheet.UsedRange.Rows.Count

ActiveWorkbook.PivotCaches.add(SourceType:=xlDatabase, SourceData:= _
"Results!A1:AJ" & prows).CreatePivotTable TableDestination:="", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top