Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default counting excel files in a folder

I have the file path...

now I need to count the number of xcel files
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default counting excel files in a folder

One way is to use DIR with a loop....

Sub Macro()
Dim strFolder As String, strFile As String
Dim intCount As Integer
strFolder = "d:\"
strFile = Dir(strFolder & "*.xls", vbNormal)
Do While strFile < ""
intCount = intCount + 1
strFile = Dir
Loop
MsgBox intCount & " files found"
End Sub

--
If this post helps click Yes
---------------
Jacob Skaria


"dstiefe" wrote:

I have the file path...

now I need to count the number of xcel files

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default counting excel files in a folder

Here is another method

Sub test()
folder = "c:\temp"

Set FS = Application.FileSearch
With FS
.NewSearch
.LookIn = folder
.SearchSubFolders = False
'.Filename = "Run"
'.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks

FileCount = .Execute

If FileCount 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
Else
MsgBox "There were no files found."
End If

End With


End Sub


"dstiefe" wrote:

I have the file path...

now I need to count the number of xcel files

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default counting excel files in a folder

This is how you can do it with the Windows API.
Haven't tested, but might be the fastest way.

Option Explicit

Private Const vbDot = 46
Private Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const vbBackslash = "\"
Private Const ALL_FILES = "*.*"

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

Private Declare Function FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA)
As Long
Private Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As
Long
Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenW" (ByVal lpString As Long)
As Long

Function CountFilesInFolder(sRoot As String, _
strFile As String) As Long

Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim strFileName As String

hFile = FindFirstFile(sRoot & ALL_FILES, WFD)

If hFile < INVALID_HANDLE_VALUE Then
Do
'if a folder call method again
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) < vbDot Then
'--------------------
'DEALING WITH FOLDERS
'--------------------
CountFilesInFolder _
sRoot & TrimNull(WFD.cFileName) & vbBackslash, _
strFile
End If
Else
'------------------
'DEALING WITH FILES
'------------------
strFileName = TrimNull(WFD.cFileName)
If strFileName Like strFile Then
CountFilesInFolder = CountFilesInFolder + 1
End If
End If
Loop While FindNextFile(hFile, WFD)
End If

FindClose hFile

End Function

Function TrimNull(strString As String) As String
TrimNull = Left$(strString, lstrlen(StrPtr(strString)))
End Function


RBS



"dstiefe" wrote in message
...
I have the file path...

now I need to count the number of xcel files


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default counting excel files in a folder

dies this work inexcel 2007

when i do this ittells me that "Object doesn't support this action"

any ideas?

"Joel" wrote:

Here is another method

Sub test()
folder = "c:\temp"

Set FS = Application.FileSearch
With FS
.NewSearch
.LookIn = folder
.SearchSubFolders = False
'.Filename = "Run"
'.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks

FileCount = .Execute

If FileCount 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
Else
MsgBox "There were no files found."
End If

End With


End Sub


"dstiefe" wrote:

I have the file path...

now I need to count the number of xcel files

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pulling pdf files from general folder to specific folder [email protected] Excel Discussion (Misc queries) 2 September 8th 09 09:41 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven[_2_] Excel Programming 1 January 24th 06 04:23 AM
Counting files in a folder Hawki Excel Programming 7 April 14th 04 05:54 PM
counting files in a folder jefe Excel Programming 2 March 2nd 04 07:36 PM


All times are GMT +1. The time now is 12:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"