Solved

Excel macro to create hundreds of workbooks with names & date from one master file, based on a template

Posted on 2013-12-18
7
294 Views
Last Modified: 2013-12-18
I have an Excel workbook called Master.xlsx.


The first row contains column headers.


The first cell in each row, starting at A2 through to A1200, contains a number, like 12345678, which is to be the new filename.


Columns B through AZ contain data I want to be written into each new workbook.


The end result is to be 1200 new files with names generated from column A each with row 2 popultaed with data from Master.xlsx.


The new files are all to be based on a template with formatting called MasterTemplate.xlsx which already has the first row populated with column headers the same as Master.xlsx.


I would like all the files to be created in folder D:\GeneratedWorkbooks


Thanks in advance!
0
Comment
Question by:snooflehammer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39728303
Try this:
Const C_TEMPLATE = "MasterTemplate.xlsx"
Const C_OUTPUT_PATH = "D:\GeneratedWorkbooks\"

Sub GenerateWorkbooks()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Dim rw As Integer
    Dim wbt As Workbook
    With ActiveSheet
        Set wbt = Workbooks.Open(C_TEMPLATE, , True, , , , , , , , , , False)
        For rw = 2 To .UsedRange.Rows.Count
            wbt.Sheets(1).Rows(2).Clear
            .Range(.Cells(rw, 2), .Cells(rw, 52)).Copy wbt.Sheets(1).Cells(2, 1)
            wbt.SaveAs C_OUTPUT_PATH & .Cells(rw, 1) & ".xlsx", , , , , , , , False
        Next
        wbt.Close
        Set wbt = Nothing
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Open in new window

If you want the copied cells to start at column B then change the end of line 13 to:
.Cells(2, 2)

Open in new window

Master.xlsm
0
 

Author Comment

by:snooflehammer
ID: 39728316
Where is this macro expecting to find MasterTemplate.xlsx ?
0
 

Author Comment

by:snooflehammer
ID: 39728388
Ignore previous. This crashes with runtime error 424 Object Required on the line "For rw = 2 To .UsedRange.Rows.Count"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39728415
Did you try the attached file or did you put the macro in your existing file?

If the latter, how/where did you implement it (I had just put it under 'ThisWorkbook'), the only reason I can think of is ActiveSheet being undefined?

Maybe you can replace that with a direct reference to the sheet where your data is, maybe just Sheet1?
0
 

Author Comment

by:snooflehammer
ID: 39728424
I put the macro code in personal.xlsx.

I didn't notice the attached file. I just tried it and it returns a pop-up dialog "400" with no other info.

Sorry for being inept, but I don't know much about coding Excel Macros.

I've got the two workbooks, master.xlsx & mastertemplate.xlsx in D:\GeneratedWorkbooks

I changed the macro code to point to the mastertemplate.xlsx file thus:
Const C_TEMPLATE = "D:\GeneratedWorkbooks\MasterTemplate.xlsx"

I suspect the issue may be the location of master.xlsx. I have tried it with that file in  MY Docs, which I think is the correct place to no avail.

How to proceed?
0
 

Author Comment

by:snooflehammer
ID: 39728429
Scratch previous message. I opened Master.xlsx & ran the macro from there & it vomited new workbooks in a most satisfactory manner.

Top job!
0
 

Author Closing Comment

by:snooflehammer
ID: 39728430
A well thought out answer that totally addressed my issue. Thank you very much
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question