bergie7isu
New Member
- Joined
- Dec 1, 2010
- Messages
- 14
This one is throwing me for a loop (apologies for the pun...please still help me!).
I'm confused about this one.
Here's what the macro does, high level.
To be clear, the macro works...does just what I want it to. However, the first time through the loop takes about 10 seconds. All subsequent loops take about 90 seconds.
Now, if I put a msgbox at the end of each loop, each loop only takes 10 seconds.
Hmm...
Any ideas?
A simplified version of my code is below.
I'm confused about this one.
Here's what the macro does, high level.
- Opens a template workbook.
- Saves a copy of the template workbook as a temp file.
- Loops through a list of other "original" workbooks:
- One at a time, it takes data from the original workbook and transfers it to the temp file.
- Closes the original workbook and renames it.
- Saves/Closes the newly populated temp file and renames it (using the name of the original).
- Repeats until all the way through the list.
- Closes the template workbook.
To be clear, the macro works...does just what I want it to. However, the first time through the loop takes about 10 seconds. All subsequent loops take about 90 seconds.
Now, if I put a msgbox at the end of each loop, each loop only takes 10 seconds.
Hmm...
Any ideas?
A simplified version of my code is below.
Code:
Sub throwingmeforaloop()
'open the template file
Workbooks.Open "G:\" & templatefilename
'update authorized docs
For Each cell In ThisWorkbook.Sheets("Flash").Range("authorizeddocs")
'start the timer
StartTime = Timer
'open the original team file
teamfilename = cell.Value & ".xlsm"
Workbooks.Open "G:\" & teamfilename
'save a copy of the template file as a temporary team file
tempfile = cell.Value & "temp.xlsm"
Workbooks(templatefilename).SaveCopyAs ("G:\" & tempfile)
Workbooks.Open "G:\" & tempfile
'ALL SORTS OF DATA TRANSFER HAPPENING HERE
'taking data from Workbooks(teamfilename) and transfering it to Workbooks(tempfile)
'close the original team file
Workbooks(teamfilename).Close (False)
'rename and move the original team file
Name "G:\" & teamfilename As "G:\" & cell.Value & "backup.xlsm"
'save and close the tempfile
Workbooks(tempfile).Close (True)
'rename and move the new team file
Name "G:\" & tempfile As "G:\" & teamfilename
'stop the timer
SecondsElapsed = Round(Timer - StartTime, 2)
Next cell
'close the template file
Workbooks(templatefilename).Close (False)
'tell the user that everything finished
ThisWorkbook.Activate
response = MsgBox("See Flash Status for results.", vbInformation, "Flash Complete!")
Workbooks(templatefilename).Close (False)
End Sub