|
'获取文件名的方法之一:Dir函数
'获得某个文件夹下的某类扩展名文件Name 和 Path,结果生成一个二维数组
Function getfileinfo(Folderpath As String)
Dim file_info() As String
Dim i%
Filename = Dir(Folderpath)
If Filename = "" Then
MsgBox "No files!"
file_info(1, 1) = ""
Exit Function
End If
i = 0
Do While Filename <> ""
i = i + 1
ReDim Preserve file_info(1 To i, 1 To 2) As String
file_info(i, 1) = Filename
file_info(i, 2) = Folderpath
Filename = Dir
Loop
getfileinfo = file_info
MsgBox "共有" & i & "个文件"
End Function
Sub test()
Dim newrow%, newcol%
Dim arr
arr = getfileinfo(ThisWorkbook.Path & "\")
newrow = UBound(arr)
newcol = UBound(arr, 2)
ThisWorkbook.Worksheets(1).Range(Cells(2, 2), Cells(1 + newrow, 1 + newcol)) = getfileinfo(ThisWorkbook.Path & "\")
End Sub
|
|