利用Excel VBA自動將xls檔轉成xlsx檔

首先,要先創建一個帶有巨集的活頁簿。 接著將活頁簿與想要轉檔的資料夾放在一起。(子資料夾的Excel檔也會自動轉檔) 將以下程式碼放入巨集中:
Dim iFile(1 To 100000) As String
Dim count As Integer
Sub xls2xlsx()
    iPath = ThisWorkbook.Path
    On Error Resume Next
    count = 0
    zdir iPath
    For i = 1 To count
        If iFile(i) Like "*.xls" And iFile(i) <> ThisWorkbook.FullName Then
            MyFile = iFile(i)
            FilePath = Replace(MyFile, ".xls", ".xlsx")
            If Dir(FilePath, 16) = Empty Then
                Set WBookOther = Workbooks.Open(MyFile)
                Application.ScreenUpdating = False
                ActiveWorkbook.SaveAs Filename:=FilePath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
                WBookOther.Close SaveChanges:=False     
                Application.ScreenUpdating = True
            End If
        End If
    Next
End Sub
Sub zdir(p)
  Set fs = CreateObject("scripting.filesystemobject")
  For Each f In fs.GetFolder(p).Files
    If f <> ThisWorkbook.FullName Then count = count + 1: iFile(count) = f
  Next
  For Each m In fs.GetFolder(p).SubFolders
      zdir m
  Next
End Sub
執行巨集,搞定。 至於舊的xls檔,可以按照時間去排序,就能快速刪除囉。

留言

這個網誌中的熱門文章

Uber Eat退款方式處理(圖文說明)

Node-RED 與Line Notify的應用說明(圖文步驟)

pandas.parser.CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'. Problem Sovling 解法之一