VBA打开没有扩展名的文件夹并保存为不同的格式

我试图写一个macros来获取文件夹中的每个文件打开,然后保存为一个xlsx文件。 我正在使用的文件没有文件扩展名,数据是制表符分隔并手动打开罚款。 我有这个代码来打开一个文件夹中的每个文件,但是我没有能够得到它打开没有文件扩展名的文件。

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 

我也不知道如何让每个文件保存为xlsx文件。

我是全新的vba,所以任何帮助将不胜感激。 谢谢

我知道这是你正在寻找的代码:

 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 

您当前的代码只查找已经有.xlsx扩展名的文件。 尝试Dir('*')

如果你只是重命名文件,那么我不会试图打开它们。 我会使用FileCopy

 FileCopy source, destination 

源和目标都是string。 例如,以下工作适用于我(在Windows XP上):

  FileCopy "testthis", "testthis.xlsx" 

然后你可以使用Kill删除旧文件

 Kill pathname