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
278 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

911 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

18 Experts available now in Live!

Get 1:1 Help Now