VBA Code For Creating New Workbooks From the Copies of A Template File

eenginar

New Member
Joined
Mar 27, 2020
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Hello brothers,

I have a database file which has over 1000 rows and 5 columns.

* I need a VBA code for creating a new workbook from the information of each row.
* For each row, i need a different workbook with same template.
* New workbooks will take their names form the database files C and A columns.
* 5 values (A, B,C,D,E column values of each row) will be a added to new workbooks.

Sample DATABASE and EXAMPLE TEMPLATE pic are as followings:

I hope you can help me:)
 

Attachments

  • DATABASE.jPG
    DATABASE.jPG
    192.9 KB · Views: 11
  • EXAMPLE TEMPLATE FILE.JPG
    EXAMPLE TEMPLATE FILE.JPG
    234.9 KB · Views: 11

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this
Change "DATABASE" and "TEMPLATE" for the name of your sheeets.

VBA Code:
Sub Creating_New_Workbooks()
  Dim sh1 As Worksheet, sh2 As Worksheet, wb2 As Workbook, i As Long
  '
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Set sh1 = Sheets("DATABASE")
  Set sh2 = Sheets("TEMPLATE")
  '
  For i = 2 To sh1.Range("A" & Rows.Count).End(3).Row
    sh2.Range("B5").Value = sh1.Range("A" & i).Value
    sh2.Range("B7").Value = sh1.Range("C" & i).Value
    sh2.Range("B9").Value = sh1.Range("D" & i).Value
    sh2.Range("B11").Value = sh1.Range("B" & i).Value
    sh2.Range("C6").Value = sh1.Range("E" & i).Value
    sh2.Copy
    Set wb2 = ActiveWorkbook
    wb2.SaveAs ThisWorkbook.Path & "\" & sh1.Range("C" & i).Value & "-" & sh1.Range("A" & i).Value & ".xlsx"
    wb2.Close False
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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