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

Hi,

i want to open all excel files in a folder one by one to get some data.

i'm trying to use application.filesearch , but it does not work.

I'm using excel 2007.
can someone give me a piece of code to list all .xlsx and .xls file in a
folder?

thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default help - how to list all excel files in a folder

Hi,

This works for me and you should be able to easily adapt this for your
purpose

You need to have a reference to Microsoft Scripting Runtime

Dim FoldName As String

Sub aListFilesInFolder()

' Adding a 1 here will include sub folders
IncSub = 1
' the folder name you want to search
FoldName = "C:\My Documents"

Workbooks.Add

Range("A1") = "File Name"
Range("B1") = "Modified"
Range("C1") = "Accessed"
Range("D1") = "Created"
Range("E1") = "Size"
Range("F1") = "Path"
Range("G1") = "Type"

Range("A1:G1").Font.Bold = True

Application.DisplayAlerts = False

If IncSub = 1 Then
ListFilesInFolder FoldName, True
Else
ListFilesInFolder FoldName, False
End If

Range("B:D").HorizontalAlignment = xlCenter
Range("E1").HorizontalAlignment = xlRight
Columns("A:G").AutoFit

End Sub

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As
Boolean)

' Lists information about the files in SourceFolder
' Example: ListFilesInFolder "C:\FolderName\", True

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)

' Any folder we want to miss must be declared here ...

t1 = SourceFolder

' The following hidden folders cause an error so we exclude them
If t1 = FoldName & "System Volume Information" _
Or Left(t1, 8) = FoldName & "RECYC" Then GoTo End_Loop

r = Range("A65536").End(xlUp).Row + 1

For Each FileItem In SourceFolder.Files

On Error GoTo No_Add
Cells(r, 1).Formula = FileItem.Name
Cells(r, 2).Formula = FileItem.DateLastModified
Cells(r, 3).Formula = FileItem.DateLastAccessed
Cells(r, 4).Formula = FileItem.DateCreated
With Cells(r, 5)
.Formula = Int(FileItem.Size / 1024) & " KB"
.HorizontalAlignment = xlRight
End With
x = Len(FileItem.Name)
y = Len(FileItem.Path)
Cells(r, 6).Formula = Mid(FileItem.Path, 1, y - x)
Application.StatusBar = "Checking " & Mid(FileItem.Path, 1, y -
x)
Cells(r, 7).Formula = FileItem.Type
r = r + 1 ' next row number
No_Add:
Next FileItem

Miss_Loop:

' Include sub folders if requested by user
If IncludeSubfolders Then
For Each SubFolder In SourceFolder.SubFolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If

End_Loop:

Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing

Application.DisplayAlerts = True
On Error GoTo 0

End Sub

Rgds
Raymond

On 7 Nov, 08:02, doniy wrote:
Hi,

i want to open all excel files in a folder one by one to get some data.

i'm trying to use application.filesearch , but it does not work.

I'm using excel 2007.
can someone give me a piece of code to list all .xlsx and .xls file in a
folder?

thanks



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
Read Files from a folder into an Excel drop down list or Combo Box dhstein Excel Discussion (Misc queries) 2 January 16th 09 05:41 PM
Get list of files in a folder in Excel Khoshravan Excel Programming 2 April 29th 07 01:30 PM
Creating a List of the files in a folder in excel [email protected] Excel Discussion (Misc queries) 2 November 27th 06 08:39 PM
how do i print a list of files in a folder in excel? mikie New Users to Excel 4 June 6th 05 04:27 AM
Getting list of files in a folder to excel Dave Peterson[_3_] Excel Programming 0 July 29th 03 02:47 AM


All times are GMT +1. The time now is 06:48 AM.

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"