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
277 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now