Counta Formula / Loop

gripper

Board Regular
Joined
Oct 29, 2002
Messages
176
I found an excellent vba code to list all files in a directory onto an excel sheet. I am wondering is there a way of extending this so as it loops it will do a line count ("counta") on column "A" and return that along with the other items

Currently it returns the file name (Column A), Date Last Modified (Column B), File Size (Column C) - I would like to add the line count in Column D.

Any help would be greatly appreciated.

Sub TestListFilesInFolder()
Workbooks.Add ' create a new workbook for the file list
Range("A1").Formula = "File Name:"
Range("B1").Formula = "Date Last Modified:"
Range("C1").Formula = "Size:"
ListFilesInFolder "C:\raw_keywords\Google Keyword List\Vehicle", True
End Sub

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim r As Long
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
r = Range("A65536").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
Cells(r, 1).Formula = FileItem.Path
Cells(r, 2).Formula = FileItem.DateLastModified
Cells(r, 3).Formula = FileItem.Size
r = r + 1
Next FileItem
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
Columns("A:H").AutoFit
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
i would put it here


For Each FileItem In SourceFolder.Files
Cells(r, 1).Formula = FileItem.Path
Cells(r, 2).Formula = FileItem.DateLastModified
Cells(r, 3).Formula = FileItem.Size
counta = counta + 1
r = r + 1
Next FileItem


at the top of this sub declare

dim counta as integer

counta = 1
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,904
Members
452,948
Latest member
Dupuhini

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