اذا كنت تريد من اكسل ان يدرج لك كل ملفات مجلد معين
فقط اعطه عنوانه واضغظ الزر في هذا الملف
CODE
Option Explicit
Sub ListFiles()
Dim sPath As String
Dim aFiles() As String
Dim lFileCnt As Long
sPath = Cells(1, 1)
'===========================
If Len(Dir(sPath, vbDirectory)) = 0 Then
MsgBox "No such folder exists!", vbExclamation
Exit Sub
End If
Range("a4").CurrentRegion.ClearContents
On Error GoTo ErrHandler
Call GeT_Dirs(sPath, aFiles, lFileCnt)
If lFileCnt > 0 Then
Range("A5:C5").Value = Array("File's Name", "Size(Kb)", "Created / Modified")
Columns("B").NumberFormat = "#,##0.00"
Columns("C").NumberFormat = "d/m/yy h:mm AM/PM"
Range("A6").Resize(UBound(aFiles, 2), UBound(aFiles, 1)).Value = Application.Transpose(aFiles)
Columns("A:C").AutoFit
Else
MsgBox "No files found!", vbExclamation
End If
ExitSub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume ExitSub
End Sub
Sub GeT_Dirs(ByVal sCurrDir As String, ByRef aFiles() As String, ByRef lFileCnt As Long)
'Declare the variables
Dim sFileName As String
Dim sPathAndName As String
Dim aDirs() As String
Dim lDirCnt As Long
Dim i As Long
'Make sure the path to the current folder ends with a backslash
If Right(sCurrDir, 1) <> "" Then
sCurrDir = sCurrDir & ""
End If
'Get the files
sFileName = Dir(sCurrDir & "*.*", vbDirectory)
While Len(sFileName) > 0
If Left(sFileName, 1) <> "." Then
sPathAndName = sCurrDir & sFileName
If (GetAttr(sPathAndName) And vbDirectory) = vbDirectory Then
'Store found folders in array
lDirCnt = lDirCnt + 1
ReDim Preserve aDirs(1 To lDirCnt)
aDirs(lDirCnt) = sPathAndName
Else
'Store found files in array
lFileCnt = lFileCnt + 1
ReDim Preserve aFiles(1 To 3, 1 To lFileCnt)
aFiles(1, lFileCnt) = sFileName
aFiles(2, lFileCnt) = FileLen(sPathAndName) / 1000
aFiles(3, lFileCnt) = FileDateTime(sPathAndName)
End If
End If
sFileName = Dir
Wend
'Process the found folders, recursively
For i = 1 To lDirCnt
Call GeT_Dirs(aDirs(i), aFiles, lFileCnt)
Next i
End Sub
Private Sub CommandButton1_Click()
ListFiles
End Sub