① 用EXCEL VBA获取指定目录下的文件名(包括文件夹名)
1、首先利用快捷键“Windows键+R”打开“运行”窗口
② excel怎样用vba自动提取文件夹内的文件名
excel中用vba实现自动提取文件夹内的文件名的方法如下:
1、新建一个vba宏脚本
③ VBA获取某文件夹下所有文件和子文件目录的文件
【引用位置】 https://blog.csdn.net/pashine/article/details/42100237
'-------------------------------------------
'获取某文件夹下的所有Excel文件
'-------------------------------------------
Sub getExcelFile(sFolderPath As String)
On Error Resume Next
Dim f As String
Dim file() As String
Dim x
k = 1
ReDim file(1)
file(1) = sFolderPath & ""
End Sub
'-------------------------------------------
'获取某文件夹下的所有文件和子目录下的文件
'-------------------------------------------
Sub getAllFile(sFolderPath As String)
'Columns(1).Delete
On Error Resume Next
Dim f As String
Dim file() As String
Dim i, k, x
x = 1
i = 1
k = 1
ReDim file(1 To i)
file(1) = sFolderPath & ""
'-- 获得所有子目录
Do Until i > k
f = Dir(file(i), vbDirectory)
Do Until f = ""
If InStr(f, ".") = 0 Then
k = k + 1
ReDim Preserve file(1 To k)
file(k) = file(i) & f & ""
End If
f = Dir
Loop
i = i + 1
Loop
'-- 获得所有子目录下的所有文件
For i = 1 To k
f = Dir(file(i) & " . ") '通配符 . 表示所有文件,*.xlsx Excel文件
Do Until f = ""
'Range("a" & x) = f
Range("a" & x).Hyperlinks.Add Anchor:=Range("a" & x), Address:=file(i) & f, TextToDisplay:=f
x = x + 1
f = Dir
Loop
Next
End Sub
④ vba获取路径下所有文件名和对应的文件路径,并且显示到下面表格中。怎么做
一、所需DOS命令
dir [drive:][path] /b > [drive:][path]filename
二、应用实例
如下图,在D盘中,有一个名称为“TQIPC”的文件夹,这个文件夹里面,存放了很多的照片!
看到了没有,文件夹中的照片名称,自动就显示在excel表格中了!
该dos命令非常强大,用好dos命令,可省去我们很多的功夫。
三、知识扩展
下面,再给大家一个例子,即,自动根据照片(根据人物名称),建立文件夹。
如果某个文件夹下面,您存放的是某些人物的照片,并且,每个人物的照片,都使用其名字来命名;这个时候,如何要自动根据人物的自动建立其对应的文件夹,也相当方便!
操作的方法是,先根据上述的方法,获取所有人物的照片的名称,生成EXCEL文件;
然后,在人物名称列前面,插入一列,并输入MD;
接下来,使用查找替换功能,将excel文件中的人物名称,去掉扩展名(只保留名字);
最后,将excel的内容,复制粘贴到记事本中,现在,我们就得到了形如:“MD人物名称”这样的命令格式了。(md 文件名称,该命令格式,是使用来建立文件夹的命令,上述的操作,都是为此做准备!)
将记事本文件保存成为形如:124.bat格式的文件,最后,双击此文件,文件夹名称就自动建立好了!
⑤ Excel中VBA提取文件夹名称的方法
文章介绍excel中使用vba提取文件名的操作步骤。根据需要自行修改vba提取文件名的路径和存放单元格即可。
在excel中使用VBA编写代码,可以轻松的提取某个文件夹下面的所有文件名。
比如笔者在F盘下面建立了一个文件夹,文件夹的名称是:office教程网,现在想将“office教程网”这个文件夹下面的所有文件名提取出来放在当前工作表的C列。
具体的vba提取文件名的操作如下:
1.按下ALT+F11,打开VBE编辑器。
2.执行“插入——模块”,插入模块1。
3.在右边的代码编辑窗口,复制下面的代码,然后单击“绿色箭头”或者快捷键F5键,运行代码。
Private Sub vba提取文件名()
Dim FileName As String
Dim i As Long
FileName = Dir("F:\office教程网\*.*")
i = 0
Range("C:C").ClearContents
Do While FileName > ""
i = i + 1
Cells(i, 3) = FileName
FileName = Dir
Loop
End Sub
4.关闭VBE窗口,回到工作表中,可以在C列看到F盘“office教程网”文件夹下面所有的文件名全部罗列在C列了。
关于上面的vba提取文件名的代码,请在实际使用时,根据需要修改提取文件名的路径(F:\office教程网\*.*),以及存放在C列的位置(Cells(i, 3))。
⑥ 用EXCEL VBA获取指定目录下的文件名(包括文件夹名)
1、首先利用快捷键“Windows键+R”打开“运行”窗口
⑦ VBA 如何取C盘下和C盘文件夹下所有Excel文件名称,并将这些文件名放到Excel文件上。
VBA遍历文件夹常用有三种方法,这三种方法中,filesearch不适合2007和2010版本,而且速度比较慢,递归法速度也慢。只有用DIR加循环的方法,速度飞快。下面是三种方法的代码:
1、filesearch法
Sub test3()
Dim wb As Workbook
Dim i As Long
Dim t
t = Timer
With Application.FileSearch '调用fileserch对象
.NewSearch '开始新的搜索
.LookIn = ThisWorkbook.path '设置搜索的路径
.SearchSubFolders = True '搜索范围包括 LookIn 属性指定的文件夹中的所有子文件夹
.Filename = "*.xls" '设置搜索的文件类型
' .FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then '如果找到文件
For i = 1 To .FoundFiles.Count
'On Error Resume Next
Cells(i, 1) = .FoundFiles(i) '把找到的文件放在单元格里
Next i
Else
MsgBox "没找到文件"
End If
End With
MsgBox Timer - t
End Sub
2、递归法
Sub Test()
Dim iPath As String, i As Long
Dim t
t = Timer
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "请选择要查找的文件夹"
If .Show Then
iPath = .SelectedItems(1)
End If
End With
If iPath = "False" Or Len(iPath) = 0 Then Exit Sub
i = 1
Call GetFolderFile(iPath, i)
MsgBox Timer - t
MsgBox "文件名链接获取完毕。", vbOKOnly, "提示"
End Sub
Private Sub GetFolderFile(ByVal nPath As String, ByRef iCount As Long)
Dim iFileSys
'Dim iFile As Files, gFile As File
'Dim iFolder As Folder, sFolder As Folders, nFolder As Folder
Set iFileSys = CreateObject("Scripting.FileSystemObject")
Set iFolder = iFileSys.GetFolder(nPath)
Set sFolder = iFolder.SubFolders
Set iFile = iFolder.Files
With ActiveSheet
For Each gFile In iFile
' .Hyperlinks.Add anchor:=.Cells(iCount, 1), Address:=gFile.path, TextToDisplay:=gFile.Name
iCount = iCount + 1
Next
End With
'递归遍历所有子文件夹
For Each nFolder In sFolder
Call GetFolderFile(nFolder.path, iCount)
Next
End Sub
3、dir循环法
Sub Test() '使用双字典,旨在提高速度
Dim MyName, Dic, Did, i, t, F, TT, MyFileName
'On Error Resume Next
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", 0, 0)
If Not objFolder Is Nothing Then lj = objFolder.self.path & "\"
Set objFolder = Nothing
Set objShell = Nothing
t = Time
Set Dic = CreateObject("Scripting.Dictionary") '创建一个字典对象
Set Did = CreateObject("Scripting.Dictionary")
Dic.Add (lj), ""
i = 0
Do While i < Dic.Count
Ke = Dic.keys '开始遍历字典
MyName = Dir(Ke(i), vbDirectory) '查找目录
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Ke(i) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
Dic.Add (Ke(i) & MyName & "\"), "" '就往字典中添加这个次级目录名作为一个条目
End If
End If
MyName = Dir '继续遍历寻找
Loop
i = i + 1
Loop
Did.Add ("文件清单"), "" '以查找D盘下所有EXCEL文件为例
For Each Ke In Dic.keys
MyFileName = Dir(Ke & "*.xls")
Do While MyFileName <> ""
Did.Add (Ke & MyFileName), ""
MyFileName = Dir
Loop
Next
For Each Sh In ThisWorkbook.Worksheets
If Sh.Name = "XLS文件清单" Then
Sheets("XLS文件清单").Cells.Delete
F = True
Exit For
Else
F = False
End If
Next
If Not F Then
Sheets.Add.Name = "XLS文件清单"
End If
Sheets("XLS文件清单").[A1].Resize(Did.Count, 1) = WorksheetFunction.Transpose(Did.keys)
TT = Time - t
MsgBox Minute(TT) & "分" & Second(TT) & "秒"
End Sub
⑧ Excel VBA列出某文件夹下子文件夹及文件名
遍历文件夹 并列出文件 & 文件夹 名 代码如下:
在文件夹内 新建 个 Excel文件
Excel文件内 按 Alt+F11 视图--代码窗口, 把如下代码复制进去, F5运行
Sub遍历文件夹()
'OnErrorResumeNext
Dimfn(1To10000)AsString
Dimf,i,k,f2,f3,x
Dimarr1(1To100000,1To1)AsString,qAsInteger
Dimt
t=Timer
fn(1)=ThisWorkbook.path&""
i=1:k=1
DoWhilei<UBound(fn)
Iffn(i)=""ThenExitDo
f=Dir(fn(i),vbDirectory)
Do
IfInStr(f,".")=0Andf<>""Then
k=k+1
fn(k)=fn(i)&f&""
EndIf
f=Dir
LoopUntilf=""
i=i+1
Loop
'*******下面是提取各个文件夹的文件***
Forx=1ToUBound(fn)
Iffn(x)=""ThenExitFor
f3=Dir(fn(x)&"*.*")
DoWhilef3<>""
q=q+1
arr1(q,1)=fn(x)&f3
f3=Dir
Loop
Nextx
ActiveSheet.UsedRange=""
Range("a1").Resize(q)=arr1
MsgBoxFormat(Timer-t,"0.00000")
EndSub
效果如图:
⑨ 用VBA代码如何获得指定文件夹内的所有子文件夹名称
Set fso=CreateObject("Scripting.FileSystemObject")
Set folder=fso.GetFolder("D:")
dim a() as string
dim b
b=1
for each thing in folder.subfolders
addfolder fso,thing,a,b
next
msgbox b
function addfolder(byref fso,byref dir,byref a,byref b)
set folder=fso.getfolder(dir)
a(b)=dir
b=b+1
For Each thing in folder.SubFolders
set folder=fso.getfolder(thing)
a(b)=thing
b=b+1
addfolder fso,thing,a,b
Next
end function
⑩ vba 遍历指定文件夹(含子目录)获取文件名,哪种方法速度最快
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'这里很关键,决定宏执行快慢的关键
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'打开目录选择框
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "请选择目录"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'取消选择
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'指定过滤的文件后缀
myExtension = "*.xls*"
'遍历全路径
myFile = Dir(myPath & myExtension)
'循环处理每一个文件
Do While myFile <> ""
'打开
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'确保工作簿被打开,在处理下一个文件时
DoEvents
'设置背景色
wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)
'保存工作簿
wb.Close SaveChanges:=True
'确保工作簿被关闭,在处理下一个文件时
DoEvents
'接着处理下一个
myFile = Dir
Loop
'提示处理完成
MsgBox "处理完成!"
ResetSettings:
'恢复设置
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub