2016-08-11 76 views
0

我做了一個代碼,幫助我在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 
+0

http://www.mrexcel.com/forum /excel-questions/662460-visual-basic-applications-savecopyas-how-can-i-save-copy-xlsx.html我不認爲你可以使用SaveCopyAs以不同的格式保存副本,所以如果你有一個xls的副本只能是xls。只有將擴展名更改爲xlsx,才能更改格式。 –

+0

您正在打開來自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

+0

@TimWilliams我嘗試了V的情況下,但SaveAs也沒有工作 –

回答

2

你試圖打開一個xls文件並將其保存爲xlsx文件,無需任何轉換。要正確地將文件轉換爲xlsx你需要包括正確FileFormat

wb.SaveAs "C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\ETTJ\" & setnameETTJ(myFile) & ".xlsx", _ 
FileFormat:=xlOpenXMLWorkbook, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges 
wb.Close SaveChanges:=False 
0

我遇到了類似的情況,我所做的就是這個

Set WBDesiredToConvert = ThisWorkbook 
WBDesiredToConvert.SaveAs ThisWorkbook.Path & "\" & "MacroEnabled", 52 
相關問題