How to Efficient way for create Multiple Workbooks

Guna13

Board Regular
Joined
Nov 22, 2019
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

We have a requirement to create multiple workbooks containing one-line questions and answers to share with our internal supplier team. We are looking for a fast and efficient way to generate these workbooks. Can anyone provide guidance or suggest techniques to streamline this process? (For my requirement i need to create more than 1500+ Files )

Sub CreateWorkbooksForEachFilter()
Dim startTime As Double, endTime As Double
Dim durationSeconds As Double, durationHours As Integer, durationMinutes As Integer, durationSecondsLeft As Integer
Dim durationString As String

' Record the start time
startTime = Timer

Dim wbSource As Workbook, wbNew As Workbook
Dim wsTemplate As Worksheet, wsValidation As Worksheet, wsBPCode As Worksheet
Dim filterValue As Range, uniqueValues As Range
Dim lastRow As Long, newRow As Long

' Disable screen updating and calculation to improve performance
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False

' Set the source workbook
Set wbSource = ThisWorkbook

Set wsTemplate = wbSource.Sheets("Template")
Set wsValidation = wbSource.Sheets("Validation")

If wsTemplate.AutoFilterMode Then wsTemplate.AutoFilterMode = False
If wsValidation.AutoFilterMode Then wsValidation.AutoFilterMode = False

' Define the range of unique values in column C of the Validation sheet
Set uniqueValues = wsValidation.Range("C2:C" & wsValidation.Cells(wsValidation.Rows.Count, "C").End(xlUp).Row).SpecialCells(xlCellTypeConstants)

' Loop through each unique value in column C of the Validation sheet
For Each filterValue In uniqueValues
' Filter the Template sheet based on the current value
wsTemplate.Range("A5:AX5").AutoFilter Field:=1, Criteria1:=filterValue.Value

' Find the last row of filtered data
lastRow = wsTemplate.Cells(wsTemplate.Rows.Count, "A").End(xlUp).Row
' Get the value from column E of the last row
Dim eValue As Variant
eValue = wsTemplate.Cells(lastRow, "E").Value
emailAdr = wsTemplate.Cells(lastRow, "M").Value

' Check if there is data to copy
If lastRow > 5 Then ' Assuming data starts from row 3
' Create a new workbook
Set wbNew = Workbooks.Add

Set wsBPCode = wbNew.Sheets(1)
wsBPCode.Name = filterValue.Value

wsTemplate.Range("A1:AC" & lastRow).Copy Destination:=wsBPCode.Cells(1, 1)

For Each ws In wbSource.Sheets
If ws.Name <> "Template" And ws.Name <> "Validation" Then
' Copy the sheet to the new workbook
ws.Copy After:=wbNew.Sheets(wbNew.Sheets.Count)

' Activate the copied sheet
Set wsCopied = wbNew.Sheets(wbNew.Sheets.Count)
wsCopied.Activate

' Remove gridlines and autofit columns
ActiveWindow.DisplayGridlines = False
wsCopied.Columns.AutoFit
End If
Next ws

fileName = filterValue.Value & "-" & eValue & ".xlsx"
wbNew.SaveAs "C:\Test\E-Invoice\" & fileName
wbNew.Close False ' Close the workbook without saving changes

wsValidation.Cells(filterValue.Row, "E").Value = "C:\Test\E-Invoice\" & fileName
wsValidation.Cells(filterValue.Row, "F").Value = emailAdr
End If

wsTemplate.AutoFilterMode = False

DoEvents
Next filterValue

endTime = Timer

durationSeconds = endTime - startTime

durationHours = Int(durationSeconds / 3600)
durationSecondsLeft = durationSeconds Mod 3600
durationMinutes = Int(durationSecondsLeft / 60)
durationSecondsLeft = durationSecondsLeft Mod 60

durationString = Format(durationHours, "00") & ":" & Format(durationMinutes, "00") & ":" & Format(durationSecondsLeft, "00")

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

MsgBox "Workbooks created successfully." & vbCrLf & _
"Total Duration: " & durationString, vbInformation
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How to create 1000+ Workbook, based on Cell value. even i have 1000 Records in Macro Sheet. how to create this workbook?
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,612
Members
449,109
Latest member
Sebas8956

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