Sub Open_All_XLS_in_Folder()
Application.ScreenUpdating = False
Dim DataFileLocation As String 'folder location
Dim Workbookname As String
Dim XLS_filename(78) As String 'max right now is 78 change to taste
Dim Filename_Convention As String
Dim New_Workbook As Workbook
Filename_Convention = "*.xls"
DataFileLocation = "C:\" 'must end in "\"
With Application.FileSearch
.NewSearch
.LookIn = DataFileLocation
.Filename = "*.xls"
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If .Execute > 0 Then
num_files = .FoundFiles.Count
'the following for statement
For i = 1 To num_files
XLS_filename(i) = GetShortName(.FoundFiles(i))
Next i
End If
End With
If num_files = 0 Then
MsgBox ("No XLS files found")
Error (665)
End If
For j = 1 To num_files Step 1
Workbooks.Open DataFileLocation & XLS_filename(j)
Set New_Workbook = Workbooks(XLS_filename(j))
'put your stuff here
New_Workbook.SaveAs DataFileLocation & Replace(XLS_filename(j), ".xls", ".2.xls")
New_Workbook.Close
Next j
Application.ScreenUpdating = True
End Sub
Function FileNamePosition(sFullName As String) As Integer
Dim bFound As Boolean
Dim nPosition As Integer
bFound = False
nPosition = Len(sFullName)
Do While bFound = False
' Make sure we were not dealt a
' zero-length string
If nPosition = 0 Then Exit Do
'End If
' We are looking for the first "\"
' from the right.
If Mid(sFullName, nPosition, 1) = "\" Then
bFound = True
Else
' Working right to left
nPosition = nPosition - 1
End If
Loop
If bFound = False Then
FileNamePosition = 0
Else
FileNamePosition = nPosition
End If
End Function
Function GetShortName(sLongName As String) As String
Dim sPath As String
Dim sShortName As String
BreakdownName sLongName, sShortName, sPath
GetShortName = sShortName
End Function
Sub BreakdownName(sFullName As String, _
ByRef sName As String, _
ByRef sPath As String)
Dim nPos As Integer
'Find out where the filename begins
nPos = FileNamePosition(sFullName)
If nPos > 0 Then
sName = Right(sFullName, Len(sFullName) - nPos)
sPath = Left(sFullName, nPos - 1)
Else
'invalid sFullname -don't change anything
End If
End Sub