0% found this document useful (0 votes)
15 views2 pages

Ledgers Required Code 1

This VBA code loops through multiple Excel worksheets, extracts data from each, creates a pivot table from the data, and saves the pivot tables to a new workbook. It defines variables to reference the source and destination workbooks and worksheets, identifies the data range, creates a pivot cache and pivot table, and adds fields to the rows and values areas of each pivot table.

Uploaded by

Tarunvd03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Ledgers Required Code 1

This VBA code loops through multiple Excel worksheets, extracts data from each, creates a pivot table from the data, and saves the pivot tables to a new workbook. It defines variables to reference the source and destination workbooks and worksheets, identifies the data range, creates a pivot cache and pivot table, and adds fields to the rows and values areas of each pivot table.

Uploaded by

Tarunvd03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sub CreatePivotTables()

Dim SourceFolder As String


Dim SourceFileName As Variant
Dim SourceWorkbook As Workbook
Dim SourceWorksheet As Worksheet
Dim NewWorkbook As Workbook
Dim PivotTable As PivotTable
Dim PivotField As PivotField
Dim PivotCache As PivotCache
Dim LastRow As Long
Dim LastColumn As Long
Dim StartRow As Long
Dim DataRange As Range
Dim DestSheet As Worksheet

' Define the source folder containing your 5 worksheets


SourceFolder = ThisWorkbook.Path & "\"

' Create a new workbook to store the pivot tables


Set NewWorkbook = Workbooks.Add
NewWorkbook.SaveAs SourceFolder & "PivotTables.xlsx"

' Loop through each source worksheet


For Each SourceFileName In Array("17-18_DS.xlsx", "18-19_DS.xlsx", "19-
20_DS.xlsx", "20-21_DS.xlsx", "21-22_DS.xlsx", "22-23_DS.xlsx")
On Error Resume Next ' Enable error handling

' Open the source workbook


Set SourceWorkbook = Workbooks.Open(SourceFolder & SourceFileName)

On Error GoTo 0 ' Disable error handling

' Check if the workbook was successfully opened


If Not SourceWorkbook Is Nothing Then
' Set the source worksheet (assuming it's named "Complete 2A")
Set SourceWorksheet = SourceWorkbook.Sheets("Complete 2A")

' Define the starting row from which data should be fetched (e.g., 2nd
row)
StartRow = 2

' Find the last row and last column with data in the source worksheet
LastRow = SourceWorksheet.Cells(SourceWorksheet.Rows.Count,
1).End(xlUp).Row
LastColumn = SourceWorksheet.Cells(1,
SourceWorksheet.Columns.Count).End(xlToLeft).Column

' Define the data range


Set DataRange = SourceWorksheet.Range(SourceWorksheet.Cells(StartRow,
1), SourceWorksheet.Cells(LastRow, LastColumn))

' Create a PivotCache based on the data range


Set PivotCache = NewWorkbook.PivotCaches.Create(SourceType:=xlDatabase,
SourceData:=DataRange)

' Create a new sheet in the new workbook with the same name as the
source file
Set DestSheet = NewWorkbook.Sheets.Add
DestSheet.Name = Left(SourceFileName, Len(SourceFileName) - 5) '
Removes ".xlsx" from the sheet name

' Create a PivotTable on the new sheet


Set PivotTable =
PivotCache.CreatePivotTable(TableDestination:=DestSheet.Cells(3, 1),
TableName:="PivotTable")

' Add "GSTIN" field to the Rows section


Set PivotField = PivotTable.PivotFields("GSTIN")
PivotField.Orientation = xlRowField

' Add fields to the Values section


Set PivotField = PivotTable.PivotFields("IGST")
PivotField.Orientation = xlDataField

Set PivotField = PivotTable.PivotFields("CGST")


PivotField.Orientation = xlDataField

Set PivotField = PivotTable.PivotFields("SGST")


PivotField.Orientation = xlDataField

Set PivotField = PivotTable.PivotFields("CESS")


PivotField.Orientation = xlDataField

' Close the source workbook


SourceWorkbook.Close SaveChanges:=False
End If
Next SourceFileName

' Save the pivot tables workbook


NewWorkbook.Save
End Sub

You might also like