count files in folder

kamal1jacq

New Member
Joined
Feb 4, 2010
Messages
12
Sub Demo()
Call CountfilesInFolder("C:\Users\kamal.j.kumar\Documents\CDP", "*.xls*")
End Sub

Hi,
In the above macro, I get the following compile error: sub or function not defined. Why ?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Here is a function that you can call to get the file count
Code:
[table="width: 500"]
[tr]
	[td]Function FileCount(ByVal Path As String, WildcardFileName As String) As Long
  Dim Filename As String
  If Right(Path, 1) <> "\" Then Path = Path & "\"
  Filename = Dir(Path & WildcardFileName)
  Do While Len(Filename)
    FileCount = FileCount + 1
    Filename = Dir()
  Loop
End Function[/td]
[/tr]
[/table]
Simply pass it the path to the files and the wildcard filename (same order as you used in your CountfilesInFolder). Here is a demo using the path and wildcard filename that your original posting used...
Code:
[table="width: 500"]
[tr]
	[td]Sub Demo()
  MsgBox FileCount("C:\Users\kamal.j.kumar\Documents\CDP", "*.xls*")
End Sub[/td]
[/tr]
[/table]
 
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