My macro requires me to press enter hundred times to run query (how to avoid it)

Mughees

New Member
Joined
Nov 5, 2019
Messages
4
Hi All,

I am new on this forum. I have recently developed a macro through which I am able to list all files present in the sub-folders. However, there are large number of files (in thousands) and once I run my macro, it wants me to keep my hand on enter key for nearly 5 mins. Can any one tell me a command through which I can run macro for once and it does not ask me to press enter for each row and run it once and for all. Thank you in advance.

Below is my query


Option Explicit


Sub listallfiles()


Dim objfso As Scripting.FileSystemObject
Dim objfolder As Scripting.Folder


Set objfso = CreateObject("scripting.filesystemobject")
Set objfolder = objfso.GetFolder("E:\data\2019 data")


Call getfiledetails(objfolder)




End Sub




Function getfiledetails(objfolder As Scripting.Folder)




Dim objfile As Scripting.File
Dim nextrow As Long
Dim objsubfolder As Scripting.Folder




nextrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each objfile In objfolder.Files
Cells(nextrow, 1) = objfile.Name
Cells(nextrow, 2) = objfile.Path
Cells(nextrow, 3) = objfile.Size
Cells(nextrow, 4) = objfile.Type
Cells(nextrow, 5) = objfile.DateCreated
Cells(nextrow, 6) = objfile.DateLastModified
nextrow = nextrow + 1
Next


For Each objsubfolder In objfolder.SubFolders
Call getfiledetails(objsubfolder)

Next


For Each objfile In objfolder.Files
MsgBox objfile.Name
Next














End Function
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try
Rich (BB code):
Option Explicit

Sub listallfiles()

Dim objfso As Scripting.FileSystemObject
Dim objfolder As Scripting.Folder

Set objfso = CreateObject("scripting.filesystemobject")
Set objfolder = objfso.GetFolder("E:\data\2019 data")

Call getfiledetails(objfolder)

End Sub


Function getfiledetails(objfolder As Scripting.Folder)

Dim objfile As Scripting.File
Dim nextrow As Long
Dim objsubfolder As Scripting.Folder

nextrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each objfile In objfolder.Files
Cells(nextrow, 1) = objfile.Name
Cells(nextrow, 2) = objfile.Path
Cells(nextrow, 3) = objfile.Size
Cells(nextrow, 4) = objfile.Type
Cells(nextrow, 5) = objfile.DateCreated
Cells(nextrow, 6) = objfile.DateLastModified
nextrow = nextrow + 1
Next

For Each objsubfolder In objfolder.SubFolders
Call getfiledetails(objsubfolder)

Next

End Function


 
Upvote 0
Thanks alot. Matter resolved :)


Try
Rich (BB code):
Option Explicit

Sub listallfiles()

Dim objfso As Scripting.FileSystemObject
Dim objfolder As Scripting.Folder

Set objfso = CreateObject("scripting.filesystemobject")
Set objfolder = objfso.GetFolder("E:\data\2019 data")

Call getfiledetails(objfolder)

End Sub


Function getfiledetails(objfolder As Scripting.Folder)

Dim objfile As Scripting.File
Dim nextrow As Long
Dim objsubfolder As Scripting.Folder

nextrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each objfile In objfolder.Files
Cells(nextrow, 1) = objfile.Name
Cells(nextrow, 2) = objfile.Path
Cells(nextrow, 3) = objfile.Size
Cells(nextrow, 4) = objfile.Type
Cells(nextrow, 5) = objfile.DateCreated
Cells(nextrow, 6) = objfile.DateLastModified
nextrow = nextrow + 1
Next

For Each objsubfolder In objfolder.SubFolders
Call getfiledetails(objsubfolder)

Next

End Function


 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top