Hi, I have this code below:
In this code above, I would like to change "301106" to refer to the name of the worksheet where I take my data from, which is the previous worksheet in the code highlighted in blue. I have many data worksheet from codes "301106" to "301111" and I want to create a pivot table for each separate worksheet. For example, if I run the macro using a different worksheet named "301107", the entire code will change "301106" (those codes highlighted) to "301107" and so on.
Sorry if its still unclear as I am new here.
Appreciate any help.
Code:
Sub InsertPivotTable()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
[COLOR=#000080]
[/COLOR]
[COLOR=#000080]'Insert a New Blank Worksheet[/COLOR]
[COLOR=#000080]On Error Resume Next[/COLOR]
[COLOR=#000080]Application.DisplayAlerts = False[/COLOR]
[COLOR=#000080]Worksheets("PivotTable").Delete[/COLOR]
[COLOR=#000080]Sheets.Add Before:=ActiveSheet
'How can i change the below "301106" to refer to the current active sheet name before the codes below???[/COLOR]
ActiveSheet.Name = "[COLOR=#ff0000]301106[/COLOR] PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("[COLOR=#ff0000]301106[/COLOR] PivotTable")
Set DSheet = Worksheets("[COLOR=#ff0000]301106[/COLOR]")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Range("A4:G9999")
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="[COLOR=#ff0000]301106[/COLOR] PivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="[COLOR=#ff0000]301106[/COLOR] PivotTable")
'Insert Row Fields
Cells(3, 1).Select
With ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").PivotFields("GL code: [COLOR=#ff0000]301106[/COLOR] ")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").PivotFields("Company")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").PivotFields("Description")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").PivotFields("Transaction Number")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("[COLOR=#ff0000]301106 [/COLOR]PivotTable").PivotFields("Transaction Type")
.Orientation = xlRowField
.Position = 3
End With
ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").AddDataField ActiveSheet.PivotTables( _
"[COLOR=#ff0000]301106[/COLOR] PivotTable").PivotFields("Local Amount (SGD)"), "Count of Local Amount (SGD)" _
, xlCount
With ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").PivotFields( _
"Count of Local Amount (SGD)")
.Caption = "Sum of Local Amount (SGD)"
.Function = xlSum
End With
Range("B5:F17").Select
Range("F17").Activate
Selection.Style = "Comma"
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit
Range("A1").Select
'Format Pivot Table
ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("[COLOR=#ff0000]301106[/COLOR] PivotTable").TableStyle2 = "PivotStyleMedium9"
End Sub
In this code above, I would like to change "301106" to refer to the name of the worksheet where I take my data from, which is the previous worksheet in the code highlighted in blue. I have many data worksheet from codes "301106" to "301111" and I want to create a pivot table for each separate worksheet. For example, if I run the macro using a different worksheet named "301107", the entire code will change "301106" (those codes highlighted) to "301107" and so on.
Sorry if its still unclear as I am new here.
Appreciate any help.
Last edited by a moderator: