45IT.COM- 电脑学习从此开始!
DIY硬件教程攒机经验装机配置
设计Photoshop网页设计特效
系统注册表DOS系统命令其它
存储主板显卡外设键鼠内存
维修显卡CPU内存打印机
WinXPVistaWin7unix/linux
CPU光驱电源/散热显示器其它
修技主板硬盘键鼠显示器光驱
办公ExcelWordPowerPointWPS
编程数据库CSS脚本PHP
网络局域网QQ服务器
软件网络系统图像安全

Excel VBA将一个目录下的所有xls文件批量转换为xlsx文件

电脑软硬件应用网 45IT.COM 时间:2012-09-12 22:34 作者:佚名

Option Explicit

Sub xlsTOxlsx()
    Dim strFilePath As String, strFileName As String, strFileType As String
    Dim aIndex As Long, arrFileName() As String, strNewName As String

    '设置文件扩展名标识文件类型
    strFileType = ".xls"

    On Error Resume Next
    '设置文件夹路径
    strFilePath = CreateObject("shell.application").BrowseForFolder(0, "请选择文件夹", 0).self.Path
    If Err <> 0 Or InStr(1, strFilePath, "::") > 0 Then
        Err = 0
        Exit Sub
    End If

    '开始搜索文件
    strFileName = Dir(strFilePath & "*.*")
    Do While strFileName <> ""
        If LCase(Right(strFileName, Len(strFileType))) = LCase(strFileType) Then
            ReDim Preserve arrFileName(aIndex)
            arrFileName(aIndex) = strFileName
            aIndex = aIndex + 1
            'Debug.Print strFileName
        End If
        strFileName = Dir
        DoEvents
    Loop
    If aIndex = 0 Then Exit Sub

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For aIndex = LBound(arrFileName) To UBound(arrFileName)
        strNewName = Mid(arrFileName(aIndex), 1, Len(arrFileName(aIndex)) - Len(strFileType)) & ".xlsx"
        Workbooks.Open strFilePath & arrFileName(aIndex)
        ActiveWorkbook.SaveAs Filename:=strFilePath & strNewName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        Workbooks(strNewName).Close False  '关闭工作簿
        Kill strFilePath & arrFileName(aIndex)
        DoEvents
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "操作完成,共为您转换了 " & UBound(arrFileName) + 1 & " 个文件。", vbOKOnly, "完成"
End Sub

顶一下
(2)
33.3%
踩一下
(4)
66.7%
------分隔线----------------------------
无法在这个位置找到: baidushare.htm
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐知识