乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > vba自动创建文件夹

vba自动创建文件夹

作者:乔山办公网日期:

返回目录:excel表格制作


Alt+F11,打开VBA编辑器,插入一个模块,把下面的代码贴进去,按F5执行
Sub t()
Dim s As FileSearch '定义一个文件搜索对象
Set s = Application.FileSearch
s.LookIn = "c:\" '注意路径,换成你实际的路径
s.Filename = "*.*" '搜索所有zd文件
s.Execute '执行搜索
Cells.Delete '表格清空
For i = 1 To s.FoundFiles.Count
Cells(i, 1) = s.FoundFiles(i) '每一行第一列填写一个文件名
Next
End Sub

给你一个最简单的copy方法
Sub program
If Dir("F:\" & Format(Date, "YYYY-M-D"), vbDirectory) <> "" Then
MsgBox "文件夹存在"
Else
MsgBox "文件夹不存在!,系统将创建一个名为" & Format(Date, "YYYY-M-D") & "的文件夹"
MkDir "F:\" & Format(Date, "YYYY-M-D")
End If
End Sub

日期的格式可以自己该

excel中用vba实现自动提取文件夹内的文件名e799bee5baa6e78988e69d83339的方法如下:

1、新建一个vba宏脚本

2、写入如下代码:

Function GetFileList(FileSpec As String) As Variant

'   Returns an array of filenames that match FileSpec

'   If no matching files are found, it returns False


    Dim FileArray() As Variant

    Dim FileCount As Integer

    Dim FileName As String

    

    On Error GoTo NoFilesFound


    FileCount = 0

    FileName = Dir(FileSpec)

    If FileName = "" Then GoTo NoFilesFound

'   Loop until no more matching files are found

    Do While FileName <> ""

        FileCount = FileCount + 1

        ReDim Preserve FileArray(1 To FileCount)

        FileArray(FileCount) = FileName

        FileName = Dir()

    Loop

    GetFileList = FileArray

    Exit Function

'   Error handler

NoFilesFound:

    GetFileList = False

End Function

3、传入文件路径就可以获取文件名到指定的excel表格中

4、结果:



Sub 创建文件夹()
    Dim F   As String
    Dim P   As String
    Dim S   As String
    P = "D:\123"
    P = IIf(Right(P, 1) = "\", P, P & "\")
    F = Dir(P & "*.XLS*")
    Do While F <> ""
        S = Left(F, InStrRev(F, ".") - 1)
        MkDir P & S
        Name P & F As P & S & "\" & F
        F = Dir
    Loop
    MsgBox "操作完成!"
End Sub

路径自己修改下。zd

相关阅读

  • vba自动创建文件夹

  • 乔山办公网excel表格制作
  • 按来Alt+F11,打开VBA编辑器,插入一个模块,把下面的代码贴进去,按F5执行Sub t() Dim s As FileSearch 定义一个文自件搜索对象 Set s = Application.FileSearch s.LookIn = "c:" 注意路径,换成你实
关键词不能为空

ppt怎么做_excel表格制作_office365_word文档_365办公网