Hi Friends,
I am new to VBA and stuck here in the below code. Actually I have bunch of excel files in a particular folder which I need to open individually and Refresh all, Save and close. I found some code in net but the problem is code is not allowing excel to finish refresh completely so I am getting result as N/A. I want a VBA code to wait until refresh is done before it execute the next step. I went through few of the posts here but most suggestions are on "Unticking Enable Background Refresh". But in my case the option is ticked by default, wrost thing is I cant untick it . Any other help on this is hilgly appreciated.
The code I am using is as below.
Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
folderPath = "C:\Test\" 'change to suit
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Set wb = Workbooks.Open(folderPath & filename)
Sheets("Control").Select
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = False
Sheets("Slide").Select
ActiveWorkbook.Save
ActiveWindow.Close
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
I am new to VBA and stuck here in the below code. Actually I have bunch of excel files in a particular folder which I need to open individually and Refresh all, Save and close. I found some code in net but the problem is code is not allowing excel to finish refresh completely so I am getting result as N/A. I want a VBA code to wait until refresh is done before it execute the next step. I went through few of the posts here but most suggestions are on "Unticking Enable Background Refresh". But in my case the option is ticked by default, wrost thing is I cant untick it . Any other help on this is hilgly appreciated.
The code I am using is as below.
Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
folderPath = "C:\Test\" 'change to suit
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xls")
Do While filename <> ""
Set wb = Workbooks.Open(folderPath & filename)
Sheets("Control").Select
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = False
Sheets("Slide").Select
ActiveWorkbook.Save
ActiveWindow.Close
filename = Dir
Loop
Application.ScreenUpdating = True
End Sub