I would like to loop through all my excel files in the directory and resave them.
The following code:
Sub DirectoryFileLoop()
Dim fileDirectory As String
Dim FileName As String
Dim fileToOpen As Workbook
Dim strPath As String
strPath = Application.FileDialog(msoFileDialogOpen).Show
Application.ScreenUpdating = False
fileDirectory = Dir$(strPath & "*.xls", vbNormal)
FileName = Dir(fileDirectory)
Do While Len(FileName) > 0
Set fileToOpen = Workbooks.Open(fileDirectory & FileName)
With fileToOpen
.Save
.Close
End With
Debug.Print FileName
FileName = Dir
Loop
Application.ScreenUpdating = True
MsgBox ("All files have been validated")
End Sub
works just for a few first examples. Afterward I am getting an error:
Run-time error '1004' Can't move focus to the control because it is invisible, not enabled or of a type that does not accept the focus.
Tried to find some solution, and everything I found is just here:
Error: Can't move focus because it is invisible
but these hints didn't help me.
Where might be the problem here?