Reputation: 11
I am trying write a macro to get every file in a folder to open and then save as an xlsx file. The files I am working with don't have a file extension, the data is tab delimited and opens fine manually. I have this code to open every file in a folder, however I haven't been able to get it to open files without a file extension.
Sub Open_All_Files()
Dim oWbk As Workbook
Dim sFil As String
Dim sPath As String
sPath = "E:\Macro" 'location of files
ChDir sPath
sFil = Dir("*.xlsx") 'change or add formats
Do While sFil <> ""
Set oWbk = Workbooks.Open(sPath & "\" & sFil) 'opens the file
'Code to save as different file extension would go here
oWbk.Close True 'close the workbook, saving changes
sFil = Dir
Loop ' End of LOOP
End Sub
I also don't know how to get each file to save as an xlsx file.
I'm completely new to vba so any help would be greatly appreciated. Thanks
Upvotes: 1
Views: 10329
Reputation:
I understand that this is the code you are looking for:
Sub Open_All_Files()
Dim oWbk As Workbook
Dim sFil As String
Dim sPath As String
sPath = "E:\Macro" 'location of files
ChDrive "E" '-> if E is different than the current drive (if you didn't change it before, it is the drive where Office is installed)
ChDir sPath
sFil = Dir("*.*") 'change or add formats
Do While (sFil <> "" And InStr(sFil, ".") = 0)
NewFileName = sPath & "\" & sFil & ".xlsx"
On Error Resume Next
Name sFil As NewFileName 'Add extension to file
Set oWbk = Workbooks.Open(NewFileName) 'Open file as XLSX
'Do anything with the workbook
oWbk.Close True 'close the workbook, saving changes
sFil = Dir("*.*")
Loop ' End of LOOP
End Sub
Upvotes: 1
Reputation: 19367
Your current code is only looking for files that already have the .xlsx extension. Try Dir('*')
.
If you are just renaming the files then I wouldn't attempt to open them. I would use FileCopy
FileCopy source, destination
where source and destination are both strings. For example, the following worked for me (on Windows XP):
FileCopy "testthis", "testthis.xlsx"
You could then use Kill
to delete the old file
Kill pathname
Upvotes: 0