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
286 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
populate  some cells after data verification 45 26
Do Wend Macro not working 22 36
Excel 2007 VB Code for GCF 7 16
Slow calculation 2 19
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

829 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