Mlspencer08
New Member
- Joined
- Mar 17, 2022
- Messages
- 2
Hi everyone! First post here but have definitely loved getting tips/tricks over the years. Here's my problem:
I have a macro that opens a "template" Excel file, saves the file with a different name, and then does the process again...and again...and again. The Excel files are being created based on the listing of text files in a different folder. This is part of something I'm going to do later where I actually populate each of the files with the data from the text file - but first I want to just create all the Excel files - thousands of them.
The code works 100% like I would expect it to, other than the fact that it starts to slow down after 10-15 minutes. At the start, it will process/loop one file every 3-4 seconds, then after a few minutes it slows down to 6-8 seconds, and by the time I'm 20-30 minutes in it's taking a solid 15 seconds per loop.
Is there a memory cache or something that's filling up that I can clear as part of the macro? Or can I tell it to periodically stop/start so that it "resets" itself? Sorry if these are silly questions, but I feel like if the macro runs in 3 seconds the first time, there has to be a way to keep it running at that 3 seconds continually...
Here's the code:
Any advice is welcome, thank you!
I have a macro that opens a "template" Excel file, saves the file with a different name, and then does the process again...and again...and again. The Excel files are being created based on the listing of text files in a different folder. This is part of something I'm going to do later where I actually populate each of the files with the data from the text file - but first I want to just create all the Excel files - thousands of them.
The code works 100% like I would expect it to, other than the fact that it starts to slow down after 10-15 minutes. At the start, it will process/loop one file every 3-4 seconds, then after a few minutes it slows down to 6-8 seconds, and by the time I'm 20-30 minutes in it's taking a solid 15 seconds per loop.
Is there a memory cache or something that's filling up that I can clear as part of the macro? Or can I tell it to periodically stop/start so that it "resets" itself? Sorry if these are silly questions, but I feel like if the macro runs in 3 seconds the first time, there has to be a way to keep it running at that 3 seconds continually...
Here's the code:
VBA Code:
Sub TextFileLoop()
'
' TextFileLoop Macro
'
'Variable Declaration
Dim sFilePath As String
Dim sFileName As String
Dim sFileNameLength As String
Dim sExcelFileName As String
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Range("A1").Select
'Specify File Path
sFilePath = "C:\ProgramData\Kibot Agent\Data"
'Check for back slash
If Right(sFilePath, 1) <> "\" Then
sFilePath = sFilePath & "\"
End If
sFileName = Dir(sFilePath & "*.txt")
Do While Len(sFileName) > 0
'Set file name length equal to the full length less 4 so it's equal to just the symbol name
sFileNameLength = Len(sFileName) - 4
'Create variable that will then get used to save the new file name
sExcelFileName = Left(sFileName, sFileNameLength)
'Open the "Name -" analysis file template
Workbooks.Open Filename:= _
"C:\Users\MattSpencer\Desktop\Personal\Spencer Financial Group\Trading Strategies\Swing Trading\Reversal Candles\Name -.xlsx"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\MattSpencer\Desktop\Personal\Spencer Financial Group\Trading Strategies\Swing Trading\Reversal Candles\Analysis Files\Name - " & sExcelFileName & ".xlsx"
ActiveWorkbook.Close Filename:= _
"C:\Users\MattSpencer\Desktop\Personal\Spencer Financial Group\Trading Strategies\Swing Trading\Reversal Candles\Analysis Files\Name - " & sExcelFileName & ".xlsx"
'Set the fileName to the next available file
If Not (Application.VBE.MainWindow.Visible) Then
Application.VBE.MainWindow.Visible = True
Application.VBE.MainWindow.Visible = False
End If
sFileName = Dir
Loop
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Any advice is welcome, thank you!