office交流網(wǎng)--QQ交流群號(hào)及微信交流群

Access培訓(xùn)群:792054000         Excel免費(fèi)交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

微信交流群(請(qǐng)用微信掃碼)

        

Excel VBA 批量修改文件名

2020-04-24 08:00:00
tmtony8
原創(chuàng)
7141

分享一個(gè)自定義函數(shù),批量修改文件名


Dim filePath As Variant            '定義filepath為變量
Dim obj As Object                  '定義obj為變量對(duì)象
Dim fld, ff, gg                    '定義fld,ff,gg為變量
Sub getpath()
     Range("A1:Z1000").ClearContents               '清空該區(qū)域

    On Error Resume Next
     Dim shell As Variant
     Set shell = CreateObject("Shell.Application")
     Set filePath = shell.BrowseForFolder(&O0, "選擇文件夾", &H1 + &H10, "")   '獲取文件夾路徑地址
    Set shell = Nothing
   If filePath Is Nothing Then                 '檢測(cè)是否獲得有效路徑,如取消直接跳出程序
       Exit Sub
     Else
        gg = filePath.Items.Item.Path
   End If
     Set obj = CreateObject("Scripting.FileSystemObject")   '定義變量
    Set fld = obj.getfolder(gg)                            '獲取路徑
     For Each ff In fld.Files                   '遍歷文件夾里文件
        m = m + 1
         Cells(m + 1, 1) = ff.Name
         Cells(m + 1, 2) = "-------"
         Cells(m + 1, 3) = Right(ff.Name, Len(ff.Name) - 2)
       Next
 End Sub

 Sub renamefile()
 x = InputBox("原來(lái)的", "要改的")
      On Error Resume Next
      If [a2] = "" Then MsgBox "請(qǐng)點(diǎn)擊第一步": Exit Sub
       For Each ff In fld.Files                 '遍歷文件夾里的所有文件
        m = m + 1
        ff.Name = x & Cells(m + 1, 3)             '將實(shí)際文件名改成目錄中C列的對(duì)應(yīng)文件名
      Next
       MsgBox "改名已完成,請(qǐng)檢查", vbOKOnly
 End Sub

分享
文章分類
聯(lián)系我們
聯(lián)系人: 王先生
Email: 18449932@qq.com
QQ: 18449932
微博: officecn01
移動(dòng)訪問(wèn)