+ Reply to Thread
Results 1 to 5 of 5

something more fast then that...openfile-refresh-save-close

Hybrid View

  1. #1
    Registered User
    Join Date
    11-09-2021
    Location
    spain
    MS-Off Ver
    365
    Posts
    6

    something more fast then that...openfile-refresh-save-close

    hi guys!

    this macro is little bit inefficient, need 2 seconds for every file...and for >300files is slow.
    I need to update the stock price in every file (2 or 3 times every day) and then send other data calculated in theese file with new prices to another excel to compare all selected data.
    Can you optimize that?
    thanks
    Alessio

    Sub aggiorna()
    
    'With Foglio12
        Dim Myfile As String
        Dim riga As Integer
        Dim col As Integer
        Dim num_righe As Integer
        
        num_righe = 500
        col = 7 'percorsi in colonna E
        riga = 5 'parte da riga 1
    
        For riga = 5 To 350 'num_righe
            Myfile = ActiveSheet.Cells(riga, col).Value
            If Myfile <> "" And Dir(Myfile) <> "" Then
                Workbooks.Open Filename:=Myfile
                Workbooks(Dir(Myfile)).RefreshAll
                
                For Each xWs In Application.ActiveWorkbook.Sheets
                    For Each xComment In xWs.Comments
                        xComment.Delete
                    Next
                Next
                'Disable privacy settings warning
                Application.DisplayAlerts = False
                Workbooks(Dir(Myfile)).Close SaveChanges:=True
            End If
        Next riga
        
    End Sub

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: something more fast then that...openfile-refresh-save-close

    Why have you included
    Workbooks(Dir(Myfile)).RefreshAll
    That seems unnecessary here

    You should also stop all events and calculations before you start the loop and reset them after you the loop is completed
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: something more fast then that...openfile-refresh-save-close

    Your code modified
    Sub aggiorna()
    'With Foglio12
        Dim myWB        As Workbook
        Dim mySheet     As Worksheet
        Dim Myfile      As String
        Dim riga        As Integer
        Dim col         As Integer
        Dim num_righe   As Integer
        Dim xWS         As Worksheet
        Dim xComment    As Comment
        
        Set myWB = ThisWorkbook
        Set mySheet = myWB.ActiveSheet  '*  check if this the correct sheet
        num_righe = 500
        col = 7 'percorsi in colonna E
        riga = 5 'parte da riga 1
    
        With Application
            .EnableEvents = False
            .Calculation = xlCalculationManual
            .DisplayAlerts = False
            .ScreenUpdating = False
        End With
        
        For riga = 5 To 350 'num_righe
            Myfile = mySheet.Cells(riga, col).Value
            If Myfile <> "" And Dir(Myfile) <> "" Then
                Workbooks.Open Filename:=Myfile, UpdateLinks:=False
    '            Workbooks(Dir(Myfile)).RefreshAll
                
                For Each xWS In Application.ActiveWorkbook.Sheets
                    For Each xComment In xWS.Comments
                        xComment.Delete
                    Next
                Next
                'Disable privacy settings warning
                Workbooks(Dir(Myfile)).Close SaveChanges:=True
            End If
        Next riga
        
        With Application
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    11-09-2021
    Location
    spain
    MS-Off Ver
    365
    Posts
    6

    Re: something more fast then that...openfile-refresh-save-close

    thanks Kebe!!! It works very well, I need to put Workbooks(Dir(Myfile)).RefreshAll for refresh the stock price of every file...but is more fast!!

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: something more fast then that...openfile-refresh-save-close

    Okay, clear, well, happy it speeded it up

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Open, Refresh, Save, and Close linked Excel file
    By Hodor84 in forum Access Programming / VBA / Macros
    Replies: 2
    Last Post: 02-19-2017, 08:03 AM
  2. Open, Refresh, Save, and Close linked Excel file
    By Hodor84 in forum Access Tables & Databases
    Replies: 0
    Last Post: 02-15-2017, 03:23 PM
  3. Refresh all data connections, save and close
    By izk630 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2016, 10:58 AM
  4. Need macro to open, refresh, save, close 50 workbooks in a folder.
    By paulcg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2012, 09:23 AM
  5. Auto Open, Refresh, Save, Close
    By Jimmycooker in forum Excel General
    Replies: 0
    Last Post: 02-06-2006, 09:34 AM
  6. save & close after web query refresh
    By JVLin in forum Excel General
    Replies: 0
    Last Post: 09-28-2005, 12:05 PM
  7. Auto Open file VBA Refresh MS Query Save Close
    By gcutter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2005, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1