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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro Pivot Table ?, Should be easy

Status
Not open for further replies.

NateUNI

MIS
Jan 3, 2002
132
US
i have the following macro code to make a pivot table:

Sub YTD_SALES()
'
' YTD_SALES Macro
'
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"DiamondV_062002!R1C1:R1000C15").CreatePivotTable TableDestination:="", _
TableName:="PivotTable1"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
ActiveSheet.Select
ActiveSheet.Name = "YTD_SALES"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("State")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Name")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("YTD Sales")
.Orientation = xlDataField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of YTD Sales"). _
Function = xlSum
Columns("C:C").Select
Selection.NumberFormat = "$#,##0.00"
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
End Sub

My problem is that i want to export this macro and import it on another file. However when i do this i get an error because the current file that i am trying to imput it to is trying to find the source data to make the pivit table (SourceType:=xlDatabase, SourceData:="DiamondV_062002!R1C1:R1000C15"). I want the file to use the current sheet of the current document. Hope that makes sense, Thanks!
 
Well, either set a variable -
Dim Datasheet as worksheet
Set Datasheet = sheets("SheetName")
or
Set Datasheet = sheets(Sheetindex)

then amend the code to reflect this HTH
~Geoff~
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top