我做了一个代码,帮助我在optimasing在线例子的文件夹中快速保存一些文件。当我以xls格式保存文件时,一切看起来很正常,但是当我在xlsx中执行它并尝试打开保存的文件时,出现一个错误,告诉我格式已损坏。为什么在xlsx中保存不起作用,但在xls中是?
所有文件,其中以XLS在开始
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'security biass
If Worksheets("atualizador").Range("H6") <> "x" Or Worksheets("atualizador").Range("H7") <> "x" Then
Exit Sub
End If
'start folder
myPath = "C:\Users\anna.costa\Downloads\Dados\"
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'copy Worksheet's and rename
If Right(myFile, 5) <> ")" Then
Select Case Left(myFile, 1)
Case "V"
wb.SaveAs ("C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\VNA\" & setnameVNA(myFile) & ".xlsx")
wb.Close SaveChanges:=False
Case "m"
wb.SaveCopyAs ("C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\TÍTULO_PÚBLICO\" & setnameTP(myFile) & ".xls")
wb.Close SaveChanges:=False
Case "C"
wb.SaveCopyAs ("C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\ETTJ\" & setnameETTJ(myFile) & ".xlsx")
wb.Close SaveChanges:=False
End Select
End If
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
http://www.mrexcel.com/forum /excel-questions/662460-visual-basic-applications-savecopyas-how-can-i-save-copy-xlsx.html我不认为你可以使用SaveCopyAs以不同的格式保存副本,所以如果你有一个xls的副本只能是xls。只有将扩展名更改为xlsx,才能更改格式。 –
您正在打开来自VBA的xls文件...并且您想将xls文件保存到xslx而无需任何转换.....您可以尝试如下所示:'wb.SaveAs“C:\ Users \ anna.costa \ Desktop \ Dados_FIPE \ ANBIMA \ ETTJ \ “&setnameETTJ(MYFILE)&” .XLSX”, 的FileFormat:= xlOpenXMLWorkbook,AccessMode:= xlExclusive,冲突解决:= Excel.XlSaveConflictResolution.xlLocalSessionChanges wb.Close的SaveChanges:= FALSE' – Hackerman
@TimWilliams我尝试了V的情况下,但SaveAs也没有工作 –