Sub FileDetails()
Dim fso As New FileSystemObject
Dim myFolder As Folder
Dim myFile As File
Dim i As Integer
Set myFolder = fso.GetFolder("D:\ARRAY FORMULAS") ' change the folder path
If myFolder.Files.Count > 0 Then
i = 1
For Each myFile In myFolder.Files
Range("A" & i).Value = myFile.Name
Range("B" & i).Value = myFile.DateLastAccessed
Range("C" & i).Value = myFile.DateLastModified
Range("D" & i).Value = myFile.Type
Range("E" & i).Value = myFile.Size
i = i + 1
Next
Else
MsgBox "No files in the specified Folder", vbOKOnly, "No files"
End If
End Sub
Just for future readers, if you want to avoid having to set this (which is early binding), can can change vijay's code to use late binding like so:Dear Abhijeet,
VBA code to work properly, you must first create reference to the Filesystemobject.
Tools -> References -> Microsoft Scripting Runtime
Sub FileDetails()
Dim fso As Object
Dim myFolder As Folder
Dim myFile As File
Dim i As Integer
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set myFolder = fso.GetFolder("D:\ARRAY FORMULAS") ' change the folder path
If myFolder.Files.Count > 0 Then
i = 1
For Each myFile In myFolder.Files
Range("A" & i).Value = myFile.Name
Range("B" & i).Value = myFile.DateLastAccessed
Range("C" & i).Value = myFile.DateLastModified
Range("D" & i).Value = myFile.Type
Range("E" & i).Value = myFile.Size
i = i + 1
Next
Else
MsgBox "No files in the specified Folder", vbOKOnly, "No files"
End If
End Sub
Hi Luke when run this macro get Run Time Error 91 please tell me what changes need to do in this code or Setting in ExcelJust for future readers, if you want to avoid having to set this (which is early binding), can can change vijay's code to use late binding like so:
Code:Sub FileDetails() Dim fso As Object Dim myFolder As Folder Dim myFile As File Dim i As Integer Set objFSO = CreateObject("Scripting.FileSystemObject") Set myFolder = fso.GetFolder("D:\ARRAY FORMULAS") ' change the folder path If myFolder.Files.Count > 0 Then i = 1 For Each myFile In myFolder.Files Range("A" & i).Value = myFile.Name Range("B" & i).Value = myFile.DateLastAccessed Range("C" & i).Value = myFile.DateLastModified Range("D" & i).Value = myFile.Type Range("E" & i).Value = myFile.Size i = i + 1 Next Else MsgBox "No files in the specified Folder", vbOKOnly, "No files" End If End Sub
Sub FileDetails()
Dim objfso As Object
Dim myFolder As Folder
Dim myFile As File
Dim i As Integer
Set objfso = CreateObject("Scripting.FileSystemObject")
Set myFolder = objfso.GetFolder("D:\ARRAY FORMULAS") ' change the folder path
If myFolder.Files.Count > 0 Then
i = 1
For Each myFile In myFolder.Files
Range("A" & i).Value = myFile.Name
Range("B" & i).Value = myFile.DateLastAccessed
Range("C" & i).Value = myFile.DateLastModified
Range("D" & i).Value = myFile.Type
Range("E" & i).Value = myFile.Size
i = i + 1
Next
Else
MsgBox "No files in the specified Folder", vbOKOnly, "No files"
End If
End Sub
Sub SelectFolder()
Dim strFldName As String
'\\ Get folder browser
On Error Resume Next
strFldName = CreateObject("Shell.Application").BrowseForFolder(0, "Browse Folder", 0, "").Self.Path
On Error GoTo 0
'\\ Don't process further
If strFldName = "" Then
MsgBox "No Folder selected!", vbExclamation
Exit Sub
End If
'\\ Variable strFldName holds the folder path string
End Sub
Hi shrivallabha can please give combine both macro i tried but am not getting the outputYou can avoid using APIs by using Vijay's code as above.
Here's one more:
Code:Sub SelectFolder() Dim strFldName As String '\\ Get folder browser On Error Resume Next strFldName = CreateObject("Shell.Application").BrowseForFolder(0, "Browse Folder", 0, "").Self.Path On Error GoTo 0 '\\ Don't process further If strFldName = "" Then MsgBox "No Folder selected!", vbExclamation Exit Sub End If '\\ Variable strFldName holds the folder path string End Sub
Sub FileDetails()
Dim objfso As Object, myFolder As Object, myFile As Object
Dim strFldName As String
Dim i As Integer
'\\ Get folder browser
On Error Resume Next
strFldName = CreateObject("Shell.Application").BrowseForFolder(0, "Browse Folder", 0, "").Self.Path
On Error GoTo 0
'\\ Don't process further
If strFldName = "" Then
MsgBox "No Folder selected!", vbExclamation
Exit Sub
End If
Set objfso = CreateObject("Scripting.FileSystemObject")
Set myFolder = objfso.GetFolder(strFldName) ' change the folder path
If myFolder.Files.Count > 0 Then
i = 1
For Each myFile In myFolder.Files
Range("A" & i).Value = myFile.Name
Range("B" & i).Value = myFile.DateLastAccessed
Range("C" & i).Value = myFile.DateLastModified
Range("D" & i).Value = myFile.Type
Range("E" & i).Value = myFile.Size
i = i + 1
Next
Else
MsgBox "No files in the specified Folder", vbOKOnly, "No files"
End If
End Sub
Dim myFolder As Folder
Dim myFile As File
Dim myFolder As Object
Dim myFile As Object
Hi Vijay,Hi shrivallabha...
I learn new concept or you can say new way to get rid of this. Thank a lot for sharing this new way with us.
I never used Shell.application object. So i will practice this new object