[FONT="]Hi,[/FONT]
[FONT="]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
[/FONT][FONT="]Option Explicit[/FONT]
[FONT="]Sub PTOTemplateFill()[/FONT]
[FONT="]Dim LastRw As Long, Rw As Long, Cnt As Long[/FONT]
[FONT="]Dim dSht As Worksheet, tSht As Worksheet[/FONT]
[FONT="]Dim MakeBooks As Boolean, SavePath As String[/FONT]
[FONT="]Application.ScreenUpdating[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] = False 'speed up macro execution[/FONT]
[FONT="]Application.DisplayAlerts = False 'no alerts, default answers used[/FONT]
[FONT="]Set dSht = Sheets("Datasheet") 'sheet with data on it starting in row2[/FONT]
[FONT="]Set tSht = Sheets("Project Page Template") 'sheet to copy and fill out[/FONT]
[FONT="]'Option to create separate workbooks[/FONT]
[FONT="] MakeBooks = MsgBox("Create separate workbooks?" & vbLf & vbLf & _[/FONT]
[FONT="] "YES = template will be copied to separate workbooks." & vbLf & _[/FONT]
[FONT="] "NO = template will be copied to sheets within this same workbook", _[/FONT]
[FONT="] vbYesNo + vbQuestion) = vbYes[/FONT]
[FONT="]If MakeBooks Then 'select a folder for the new workbooks[/FONT]
[FONT="] MsgBox "Please select a destination for the new workbooks"[/FONT]
[FONT="] Do[/FONT]
[FONT="] With Application.FileDialog(mso[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]FileDialog[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]FolderPick[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]er)[/FONT]
[FONT="] .AllowMultiSelect = False[/FONT]
[FONT="] .Show[/FONT]
[FONT="] If .SelectedItems.Count > 0 Then 'a folder was chosen[/FONT]
[FONT="] SavePath = .SelectedItems(1) & "\"[/FONT]
[FONT="] Exit Do[/FONT]
[FONT="] Else 'a folder was not chosen[/FONT]
[FONT="] If MsgBox("Do you wish to abort?", _[/FONT]
[FONT="] vbYesNo + vbQuestion) = vbYes Then Exit Sub[/FONT]
[FONT="] End If[/FONT]
[FONT="] End With[/FONT]
[FONT="] Loop[/FONT]
[FONT="]End If[/FONT]
[FONT="]'Determine last row of data then loop through the rows one at a time[/FONT]
[FONT="] LastRw = dSht.Range("P" & Rows.Count).End(xlUp).Row[/FONT]
[FONT="] [/FONT]
[FONT="] For Rw = 2 To LastRw[/FONT]
[FONT="] tSht.Copy After:=Worksheets(Workshee[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]ts.Count) 'copy the template[/FONT]
[FONT="] With ActiveSheet 'fill out the form[/FONT]
[FONT="] [/FONT]
[FONT="] .Name = dSht.Range("P" & Rw)[/FONT]
[FONT="] .Range("AU1").Value = dSht.Range("P" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Physical Progress[/FONT]
[FONT="] .Range("L61:P61").Value = dSht.Range("AG" & Rw).Value[/FONT]
[FONT="] .Range("L62:P62").Value = dSht.Range("AH" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Financial Progress[/FONT]
[FONT="] .Range("L66:P66").Value = dSht.Range("AD" & Rw).Value[/FONT]
[FONT="] .Range("L67:P67").Value = dSht.Range("AC" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Contract Status[/FONT]
[FONT="] .Range("AC60:AG60").Value = dSht.Range("W" & Rw).Value[/FONT]
[FONT="] .Range("AC62:AG62").Value = dSht.Range("Y" & Rw).Value[/FONT]
[FONT="] .Range("AC63:AG63").Value = dSht.Range("AK" & Rw).Value[/FONT]
[FONT="] .Range("AC64:AG64").Value = dSht.Range("AL" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Contract No[/FONT]
[FONT="] .Range("AC66:AG66").Value = dSht.Range("O" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Title[/FONT]
[FONT="] .Range("CI12").Value = dSht.Range("P" & Rw).Value[/FONT]
[FONT="] .Range("CI13").Value = dSht.Range("Q" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Summary[/FONT]
[FONT="] .Range("L22:Z38").Value = dSht.Range("BL" & Rw).Value[/FONT]
[FONT="] .Range("L41:Z57").Value = dSht.Range("BM" & Rw).Value[/FONT]
[FONT="] .Range("AD23:AN31").Value = dSht.Range("BN" & Rw).Value[/FONT]
[FONT="] .Range("AD49:AN57").Value = dSht.Range("BO" & Rw).Value[/FONT]
[FONT="] End With[/FONT]
[FONT="] [/FONT]
[FONT="] If MakeBooks Then 'if making separate workbooks from filled out form[/FONT]
[FONT="] ActiveSheet.Move[/FONT]
[FONT="] ActiveWorkbook.SaveAs SavePath & Range("AU1").Value, xlNormal[/FONT]
[FONT="] ActiveWorkbook.Close False[/FONT]
[FONT="] End If[/FONT]
[FONT="] Cnt = Cnt + 1[/FONT]
[FONT="] Next Rw[/FONT]
[FONT="] dSht.Activate[/FONT]
[FONT="] If MakeBooks Then[/FONT]
[FONT="] MsgBox "Workbooks created: " & Cnt[/FONT]
[FONT="] Else[/FONT]
[FONT="] MsgBox "Worksheets created: " & Cnt[/FONT]
[FONT="] End If[/FONT]
[FONT="] [/FONT]
[FONT="]Application.ScreenUpdating[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] = True[/FONT]
[FONT="]End Sub[/FONT]
[FONT="]--------------------------[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]----------[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]----------[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]--[/FONT][FONT="]
[/FONT]
[FONT="]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
[/FONT][FONT="]Option Explicit[/FONT]
[FONT="]Sub PTOTemplateFill()[/FONT]
[FONT="]Dim LastRw As Long, Rw As Long, Cnt As Long[/FONT]
[FONT="]Dim dSht As Worksheet, tSht As Worksheet[/FONT]
[FONT="]Dim MakeBooks As Boolean, SavePath As String[/FONT]
[FONT="]Application.ScreenUpdating[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] = False 'speed up macro execution[/FONT]
[FONT="]Application.DisplayAlerts = False 'no alerts, default answers used[/FONT]
[FONT="]Set dSht = Sheets("Datasheet") 'sheet with data on it starting in row2[/FONT]
[FONT="]Set tSht = Sheets("Project Page Template") 'sheet to copy and fill out[/FONT]
[FONT="]'Option to create separate workbooks[/FONT]
[FONT="] MakeBooks = MsgBox("Create separate workbooks?" & vbLf & vbLf & _[/FONT]
[FONT="] "YES = template will be copied to separate workbooks." & vbLf & _[/FONT]
[FONT="] "NO = template will be copied to sheets within this same workbook", _[/FONT]
[FONT="] vbYesNo + vbQuestion) = vbYes[/FONT]
[FONT="]If MakeBooks Then 'select a folder for the new workbooks[/FONT]
[FONT="] MsgBox "Please select a destination for the new workbooks"[/FONT]
[FONT="] Do[/FONT]
[FONT="] With Application.FileDialog(mso[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]FileDialog[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]FolderPick[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]er)[/FONT]
[FONT="] .AllowMultiSelect = False[/FONT]
[FONT="] .Show[/FONT]
[FONT="] If .SelectedItems.Count > 0 Then 'a folder was chosen[/FONT]
[FONT="] SavePath = .SelectedItems(1) & "\"[/FONT]
[FONT="] Exit Do[/FONT]
[FONT="] Else 'a folder was not chosen[/FONT]
[FONT="] If MsgBox("Do you wish to abort?", _[/FONT]
[FONT="] vbYesNo + vbQuestion) = vbYes Then Exit Sub[/FONT]
[FONT="] End If[/FONT]
[FONT="] End With[/FONT]
[FONT="] Loop[/FONT]
[FONT="]End If[/FONT]
[FONT="]'Determine last row of data then loop through the rows one at a time[/FONT]
[FONT="] LastRw = dSht.Range("P" & Rows.Count).End(xlUp).Row[/FONT]
[FONT="] [/FONT]
[FONT="] For Rw = 2 To LastRw[/FONT]
[FONT="] tSht.Copy After:=Worksheets(Workshee[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]ts.Count) 'copy the template[/FONT]
[FONT="] With ActiveSheet 'fill out the form[/FONT]
[FONT="] [/FONT]
[FONT="] .Name = dSht.Range("P" & Rw)[/FONT]
[FONT="] .Range("AU1").Value = dSht.Range("P" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Physical Progress[/FONT]
[FONT="] .Range("L61:P61").Value = dSht.Range("AG" & Rw).Value[/FONT]
[FONT="] .Range("L62:P62").Value = dSht.Range("AH" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Financial Progress[/FONT]
[FONT="] .Range("L66:P66").Value = dSht.Range("AD" & Rw).Value[/FONT]
[FONT="] .Range("L67:P67").Value = dSht.Range("AC" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Contract Status[/FONT]
[FONT="] .Range("AC60:AG60").Value = dSht.Range("W" & Rw).Value[/FONT]
[FONT="] .Range("AC62:AG62").Value = dSht.Range("Y" & Rw).Value[/FONT]
[FONT="] .Range("AC63:AG63").Value = dSht.Range("AK" & Rw).Value[/FONT]
[FONT="] .Range("AC64:AG64").Value = dSht.Range("AL" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Contract No[/FONT]
[FONT="] .Range("AC66:AG66").Value = dSht.Range("O" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Title[/FONT]
[FONT="] .Range("CI12").Value = dSht.Range("P" & Rw).Value[/FONT]
[FONT="] .Range("CI13").Value = dSht.Range("Q" & Rw).Value[/FONT]
[FONT="] [/FONT]
[FONT="] 'Summary[/FONT]
[FONT="] .Range("L22:Z38").Value = dSht.Range("BL" & Rw).Value[/FONT]
[FONT="] .Range("L41:Z57").Value = dSht.Range("BM" & Rw).Value[/FONT]
[FONT="] .Range("AD23:AN31").Value = dSht.Range("BN" & Rw).Value[/FONT]
[FONT="] .Range("AD49:AN57").Value = dSht.Range("BO" & Rw).Value[/FONT]
[FONT="] End With[/FONT]
[FONT="] [/FONT]
[FONT="] If MakeBooks Then 'if making separate workbooks from filled out form[/FONT]
[FONT="] ActiveSheet.Move[/FONT]
[FONT="] ActiveWorkbook.SaveAs SavePath & Range("AU1").Value, xlNormal[/FONT]
[FONT="] ActiveWorkbook.Close False[/FONT]
[FONT="] End If[/FONT]
[FONT="] Cnt = Cnt + 1[/FONT]
[FONT="] Next Rw[/FONT]
[FONT="] dSht.Activate[/FONT]
[FONT="] If MakeBooks Then[/FONT]
[FONT="] MsgBox "Workbooks created: " & Cnt[/FONT]
[FONT="] Else[/FONT]
[FONT="] MsgBox "Worksheets created: " & Cnt[/FONT]
[FONT="] End If[/FONT]
[FONT="] [/FONT]
[FONT="]Application.ScreenUpdating[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="] = True[/FONT]
[FONT="]End Sub[/FONT]
[FONT="]--------------------------[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]----------[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]----------[/FONT]<wbr style="font-size: 16px; font-family: "Open Sans", sans-serif;">[FONT="]--[/FONT][FONT="]
[/FONT]