Search code examples
vbaexcelautosave

Auto-Save File while running a loop


i have a long list of local webpages which are to be parsed by a macro in Excel 2013. I was wondering if there is any way to automatically save the file every 5 minutes or so while looping through these files (>9000 local .HTM files).

Here is some code:

Sub ImportFromWebPages()

'declare here some global variables

For startnumber = 1 to 9126

    'parse the .HTM files and extract the needed data
    'while running this loop (which will take some days to finish) 
    'Excel should save the file every 5 or 10 minutes.

Next startnumber
End Sub

Any thoughts on this?


Solution

  • It may be easier to save after a certain numbers of files, say 5 for example. Please excuse my pseudocode:

    Sub ImportFromWebPages()
    
        'declare here some global variables
    
        For startnumber = 1 to 9126
    
           'parse the .HTM files and extract the needed data
           'while running this loop (which will take some days to finish) 
           'Excel should save the file every 5 or 10 minutes.
    
            If startnumber Mod 5 = 0 Then
    
                ActiveWorkbook.Save
    
            End If
    
        Next startnumber
    End Sub
    

    Please see the following links for more on Mod and ActiveWorkbook.Save.