Niv
asked on
Create worksheet based on a template from a datasheet
Hi,
I have a datasheet and a template. I need to create around 30 worksheets(in the same workbook) using the data . When I run the macro with 7 sets of data it is working fine (execution time: 3 minutes). But it is hanging if I enter more number of data.
Please find the code I am using:
Option Explicit
Sub PTOTemplateFill()
Dim LastRw As Long, Rw As Long, Cnt As Long
Dim dSht As Worksheet, tSht As Worksheet
Dim MakeBooks As Boolean, SavePath As String
Application.ScreenUpdating = False 'speed up macro execution
Application.DisplayAlerts = False 'no alerts, default answers used
Set dSht = Sheets("Datasheet") 'sheet with data on it starting in row2
Set tSht = Sheets("Project Page Template") 'sheet to copy and fill out
'Option to create separate workbooks
MakeBooks = MsgBox("Create separate workbooks?" & vbLf & vbLf & _
"YES = template will be copied to separate workbooks." & vbLf & _
"NO = template will be copied to sheets within this same workbook", _
vbYesNo + vbQuestion) = vbYes
If MakeBooks Then 'select a folder for the new workbooks
MsgBox "Please select a destination for the new workbooks"
Do
With Application.FileDialog(mso FileDialog FolderPick er)
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then 'a folder was chosen
SavePath = .SelectedItems(1) & "\"
Exit Do
Else 'a folder was not chosen
If MsgBox("Do you wish to abort?", _
vbYesNo + vbQuestion) = vbYes Then Exit Sub
End If
End With
Loop
End If
'Determine last row of data then loop through the rows one at a time
LastRw = dSht.Range("P" & Rows.Count).End(xlUp).Row
For Rw = 2 To LastRw
tSht.Copy After:=Worksheets(Workshee ts.Count) 'copy the template
With ActiveSheet 'fill out the form
.Name = dSht.Range("P" & Rw)
.Range("AU1").Value = dSht.Range("P" & Rw).Value
'Physical Progress
.Range("L61:P61").Value = dSht.Range("AG" & Rw).Value
.Range("L62:P62").Value = dSht.Range("AH" & Rw).Value
'Financial Progress
.Range("L66:P66").Value = dSht.Range("AD" & Rw).Value
.Range("L67:P67").Value = dSht.Range("AC" & Rw).Value
'Contract Status
.Range("AC60:AG60").Value = dSht.Range("W" & Rw).Value
.Range("AC62:AG62").Value = dSht.Range("Y" & Rw).Value
.Range("AC63:AG63").Value = dSht.Range("AK" & Rw).Value
.Range("AC64:AG64").Value = dSht.Range("AL" & Rw).Value
'Contract No
.Range("AC66:AG66").Value = dSht.Range("O" & Rw).Value
'Title
.Range("CI12").Value = dSht.Range("P" & Rw).Value
.Range("CI13").Value = dSht.Range("Q" & Rw).Value
'Summary
.Range("L22:Z38").Value = dSht.Range("BL" & Rw).Value
.Range("L41:Z57").Value = dSht.Range("BM" & Rw).Value
.Range("AD23:AN31").Value = dSht.Range("BN" & Rw).Value
.Range("AD49:AN57").Value = dSht.Range("BO" & Rw).Value
End With
If MakeBooks Then 'if making separate workbooks from filled out form
ActiveSheet.Move
ActiveWorkbook.SaveAs SavePath & Range("AU1").Value, xlNormal
ActiveWorkbook.Close False
End If
Cnt = Cnt + 1
Next Rw
dSht.Activate
If MakeBooks Then
MsgBox "Workbooks created: " & Cnt
Else
MsgBox "Worksheets created: " & Cnt
End If
Application.ScreenUpdating = True
End Sub
-------------------------- ---------- ---------- --
Regards,
Viv
I have a datasheet and a template. I need to create around 30 worksheets(in the same workbook) using the data . When I run the macro with 7 sets of data it is working fine (execution time: 3 minutes). But it is hanging if I enter more number of data.
Please find the code I am using:
Option Explicit
Sub PTOTemplateFill()
Dim LastRw As Long, Rw As Long, Cnt As Long
Dim dSht As Worksheet, tSht As Worksheet
Dim MakeBooks As Boolean, SavePath As String
Application.ScreenUpdating
Application.DisplayAlerts = False 'no alerts, default answers used
Set dSht = Sheets("Datasheet") 'sheet with data on it starting in row2
Set tSht = Sheets("Project Page Template") 'sheet to copy and fill out
'Option to create separate workbooks
MakeBooks = MsgBox("Create separate workbooks?" & vbLf & vbLf & _
"YES = template will be copied to separate workbooks." & vbLf & _
"NO = template will be copied to sheets within this same workbook", _
vbYesNo + vbQuestion) = vbYes
If MakeBooks Then 'select a folder for the new workbooks
MsgBox "Please select a destination for the new workbooks"
Do
With Application.FileDialog(mso
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then 'a folder was chosen
SavePath = .SelectedItems(1) & "\"
Exit Do
Else 'a folder was not chosen
If MsgBox("Do you wish to abort?", _
vbYesNo + vbQuestion) = vbYes Then Exit Sub
End If
End With
Loop
End If
'Determine last row of data then loop through the rows one at a time
LastRw = dSht.Range("P" & Rows.Count).End(xlUp).Row
For Rw = 2 To LastRw
tSht.Copy After:=Worksheets(Workshee
With ActiveSheet 'fill out the form
.Name = dSht.Range("P" & Rw)
.Range("AU1").Value = dSht.Range("P" & Rw).Value
'Physical Progress
.Range("L61:P61").Value = dSht.Range("AG" & Rw).Value
.Range("L62:P62").Value = dSht.Range("AH" & Rw).Value
'Financial Progress
.Range("L66:P66").Value = dSht.Range("AD" & Rw).Value
.Range("L67:P67").Value = dSht.Range("AC" & Rw).Value
'Contract Status
.Range("AC60:AG60").Value = dSht.Range("W" & Rw).Value
.Range("AC62:AG62").Value = dSht.Range("Y" & Rw).Value
.Range("AC63:AG63").Value = dSht.Range("AK" & Rw).Value
.Range("AC64:AG64").Value = dSht.Range("AL" & Rw).Value
'Contract No
.Range("AC66:AG66").Value = dSht.Range("O" & Rw).Value
'Title
.Range("CI12").Value = dSht.Range("P" & Rw).Value
.Range("CI13").Value = dSht.Range("Q" & Rw).Value
'Summary
.Range("L22:Z38").Value = dSht.Range("BL" & Rw).Value
.Range("L41:Z57").Value = dSht.Range("BM" & Rw).Value
.Range("AD23:AN31").Value = dSht.Range("BN" & Rw).Value
.Range("AD49:AN57").Value = dSht.Range("BO" & Rw).Value
End With
If MakeBooks Then 'if making separate workbooks from filled out form
ActiveSheet.Move
ActiveWorkbook.SaveAs SavePath & Range("AU1").Value, xlNormal
ActiveWorkbook.Close False
End If
Cnt = Cnt + 1
Next Rw
dSht.Activate
If MakeBooks Then
MsgBox "Workbooks created: " & Cnt
Else
MsgBox "Worksheets created: " & Cnt
End If
Application.ScreenUpdating
End Sub
--------------------------
Regards,
Viv
Hi it would be helpful, if you upload a sample workbook with template.
ASKER
Are you trying to create separate Workbooks as per List or trying to create separate Worksheets?
As per your requirement
As per your requirement
I need to create around 30 worksheets(in the same workbook) using the datais to create worksheet but your code is for creating worksheets then workbooks.
If you want to create worksheets in the same workbook then try below:
It took 30 seconds for deleting older sheets and creating 7 new worksheets.
It took 30 seconds for deleting older sheets and creating 7 new worksheets.
Option Explicit
Sub CreateSheetsAsPerList()
Dim LR As Long
Dim DataSh As Worksheet, Temp As Worksheet, xWs As Worksheet
Dim i As Long
Dim StartTime, EndTime
'Disable Events
With Application
.ScreenUpdating = False
.DisplayStatusBar = True
.StatusBar = "!!! Please Be Patient...Updating Records !!!"
.EnableEvents = False
.Calculation = xlManual
End With
StartTime = Now()
'Delete Older Sheets
Application.DisplayAlerts = False
For Each xWs In Application.ActiveWorkbook.Worksheets
If xWs.Name <> "Datasheet" And xWs.Name <> "Project Page Template" Then
xWs.Delete
End If
Next
Application.DisplayAlerts = True
'Set Variables
Set Temp = Worksheets("Project Page Template")
Set DataSh = Worksheets("Datasheet")
LR = DataSh.Range("P" & Rows.Count).End(xlUp).Row
'Create Sheets
For i = 2 To LR
Sheets("Project Page Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = DataSh.Range("P" & i).Value
'Physical Progress
ActiveSheet.Range("L61").Value = DataSh.Range("AG" & i).Value
ActiveSheet.Range("L62").Value = DataSh.Range("AH" & i).Value
'Financial Progress
ActiveSheet.Range("L66").Value = DataSh.Range("AD" & i).Value
ActiveSheet.Range("L67").Value = DataSh.Range("AC" & i).Value
'Contract Status
ActiveSheet.Range("AC60").Value = DataSh.Range("W" & i).Value
ActiveSheet.Range("AC62").Value = DataSh.Range("Y" & i).Value
ActiveSheet.Range("AC63").Value = DataSh.Range("AK" & i).Value
ActiveSheet.Range("AC64").Value = DataSh.Range("AL" & i).Value
'Contract No
ActiveSheet.Range("AC66").Value = DataSh.Range("O" & i).Value
'Title
ActiveSheet.Range("CI12").Value = DataSh.Range("P" & i).Value
ActiveSheet.Range("CI13").Value = DataSh.Range("Q" & i).Value
'Summary
ActiveSheet.Range("L22").Value = DataSh.Range("BL" & i).Value
ActiveSheet.Range("L41").Value = DataSh.Range("BM" & i).Value
ActiveSheet.Range("AD23").Value = DataSh.Range("BN" & i).Value
ActiveSheet.Range("AD49").Value = DataSh.Range("BO" & i).Value
Next i
Temp.Activate
Temp.Range("A1").Select
EndTime = Now()
MsgBox "!!! Total " & Worksheets.Count - 2 & " Sheets Created in " & (DateDiff("s", StartTime, EndTime)) & " Seconds !!!"
With Application
.ScreenUpdating = True
.DisplayStatusBar = True
.StatusBar = False
.EnableEvents = True
.Calculation = xlAutomatic
End With
End Sub
CreateMultipleReport_v2.xlsm
ASKER
Hi Shums,
Thank You so much for that!
It took 122 seconds to create 7 worksheet for me. I am currently trying to create 20 worksheet. I am not sure if this is a hardware issue.
I am using Excel 2013 version.
Thank You so much for that!
It took 122 seconds to create 7 worksheet for me. I am currently trying to create 20 worksheet. I am not sure if this is a hardware issue.
I am using Excel 2013 version.
There is no need to create multiple sheets. The summary sheet can be created using formulas, the only need for VBA is if you wanted to speed up the creation of reports for printing them. This is much better way than creating 30 sheets.
I've added formulas to the yellow cells as a demonstration, simply try changing the Project number in A1
Report--1-.xlsm
I've added formulas to the yellow cells as a demonstration, simply try changing the Project number in A1
Report--1-.xlsm
ASKER
Hi,
Thank You for the reply. You are right. But I need to create multiple sheets.
Thank You for the reply. You are right. But I need to create multiple sheets.
ASKER
Hi Shums,
Its still not working for me!! can you please help.
Its still not working for me!! can you please help.
Why do you need to create individual sheets? That's just inefficient, my method means that sheets can be updated automatically.
Could you upload data sheet with more than 20 in the list.
In addition to what I said before, if you add the formulas to the template sheet then your code to create the sheets would be faster
ASKER
ASKER
Roy cox,
I need to create individual sheets because each sheet will have different project map image inserted and have to be printed separately . Hope this clarify the need.
I need to create individual sheets because each sheet will have different project map image inserted and have to be printed separately . Hope this clarify the need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You :) What can be the reason why my system takes so much time to generate the sheets :(
I will try again and will come back with the results
I will try again and will come back with the results
ASKER
I have one more query. Is it possible to insert image (location map ) in the sheet using VBA if I save all the respective images in a folder with Project code as name.
Say for example if I have DS101.jpg ,DS102.jpg (Project Code column in datasheet) saved in a folder. When I run the macro it fetches the respective image for each project and insert at a specified range (Location) in the template.
By the way, It is still running
DS101.JPG
Say for example if I have DS101.jpg ,DS102.jpg (Project Code column in datasheet) saved in a folder. When I run the macro it fetches the respective image for each project and insert at a specified range (Location) in the template.
By the way, It is still running
DS101.JPG
Viv,
If solution worked as provided, I would request you to close this question and raise another question for inserting image :)
If solution worked as provided, I would request you to close this question and raise another question for inserting image :)
And Yes it is possible to assign images from specific folder to insert at specified cells.
ASKER
It took 320 seconds to generate 10 sheets..Thank You for your help