Add loop to create multiple reports

praveenlal

New Member
Joined
Oct 27, 2021
Messages
34
Office Version
  1. 2016
Platform
  1. Windows
I have multiple pivots tables in all 3 files (Book1, 2, 3). This code selects the Account Name and Year 2021 from pivot fields then copy data from pivot to Temp file, sheet Data 2021. Then again it changes pivot field to Year 2022 and paste data in Temp file, sheet Data 2022

I have created this code to save one file (Cells(1, 10) = "J1"). I have 50+ Account Names in column J. Can I add loop here to create reports for all 50+ accounts.



Sub Account_Names_Year()

Dim workbookNames As Variant
workbookNames = Array("Book1.xlsm", "Book2.xlsm", "Book3.xlsm")

Dim i As Long
For i = LBound(workbookNames) To UBound(workbookNames)

Dim wb As Workbook
Set wb = Workbooks(workbookNames(i))

Dim ws As Worksheet
Set ws = wb.Worksheets("Analysis")

Dim rootAccount As String
rootAccount = ws.Cells(1, 10).Value

Dim year As String
year = ws.Cells(1, 11).Value

Dim pt As PivotTable
For Each pt In ws.PivotTables
With pt
With .PivotFields("Account Name")
.CurrentPage = rootAccount
End With
With .PivotFields("Year")
.CurrentPage = year
End With
End With
Next pt


Dim Book1 As Workbook
Dim Book2 As Workbook
Dim Book3 As Workbook
Dim Temp As Workbook

Set Book1 = Workbooks.Open("C:\VB Code\Book1.xlsm")
Set Book2 = Workbooks.Open("C:\VB Code\Book2.xlsm")
Set Book3 = Workbooks.Open("C:\VB Code\Book3.xlsm")
Set Temp = Workbooks.Open("C:\VB Code\Template_File.xlsm")


Book1.Sheets("Analysis").Range("A13:M71").Copy
Temp.Sheets("Data (2021)").Range("B4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False

Book2.Sheets("Analysis").Range("S17:W17").Copy
Temp.Sheets("Data (2021)").Range("Z21").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False

Book3.Sheets("Analysis").Range("D12:H12").Copy
Temp.Sheets("Data (2021)").Range("Z36").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False

Book1.Sheets("Analysis").Activate


year = ws.Cells(2, 11).Value

For Each pt In ws.PivotTables
With pt
With .PivotFields("Year")
.CurrentPage = year
End With
End With
Next pt

Next i


Dim FName As String
Dim Path As String

Book1.Sheets("Analysis").Range("A13:M71").Copy
Temp.Sheets("Data (2022)").Range("B4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False

Book2.Sheets("Analysis").Range("B17:B47").Copy
Temp.Sheets("Data (2022)").Range("T5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False

Book3.Sheets("Analysis").Range("B12:M12").Copy
Temp.Sheets("Data (2022)").Range("X37").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False

ActiveWorkbook.RefreshAll

Temp.Sheets("Analysis").Activate

Range("A1").Activate

Path = "C:\VB Code\New folder\"
FName = ws.Cells(1, 10).Value & ".xlsm"

ActiveWorkbook.SaveAs Filename:=Path & FName

Application.DisplayAlerts = True

MsgBox "Cost Actuals Excel Report Created Successfully for this account"

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Getting a serious issue here, this code paste the 2022 data into 2021 sheet :cry:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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