Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Filesearch in 2007

Hi, I know little about VBA. Former employee wrote this code in 2003 but no
longer working in 2007 since 2007 dropped filesearch option.

Sub CombineFiles()

Application.DisplayAlerts = False
'On Error Resume Next

'declare variables
Dim FileCount As Long, FileNumber As Long, CurrFile As String
Dim myMacro As String, myNewFile As String, myFolder As String
Dim myFileRef As String

'assign values to variables
myMacro = ActiveWorkbook.Name
Application.Workbooks.Add
myNewFile = ActiveWorkbook.Name
Workbooks(myNewFile).Worksheets(1).Select
Range("a1").Select
myFileRef = Application.GetOpenFilename
Workbooks.Open Filename:=myFileRef
myFileRef = ActiveWorkbook.Name
myFolder = ActiveWorkbook.Path
ActiveWorkbook.Worksheets(1).Select
Range("a1").CurrentRegion.Rows(1).Copy
Workbooks(myNewFile).Activate
Range("b1").Select
ActiveSheet.Paste
Range("a1").Value = "File Name"
Range("b2").Select
Workbooks(myFileRef).Close False

'search for all Excel files in myFolder
With Application.FileSearch
.NewSearch
.LookIn = myFolder
.FileType = msoFileTypeExcelWorkbooks
.Execute
End With

'start loop to look inside each Excel file found
FileCount = Application.FileSearch.FoundFiles.Count
For FileNumber = 1 To FileCount
'give user status of macro while running
Application.StatusBar = "Searching " & FileNumber & " of " & FileCount &
" files."
'open file as read-only
Workbooks.Open Application.FileSearch.FoundFiles.Item(FileNumber) , ,
ReadOnly:=True
CurrFile = ActiveWorkbook.Name
Sheets(1).Select
Range("a1").Select
Selection.CurrentRegion.Copy
Workbooks(myNewFile).Activate
ActiveSheet.Paste
Selection.Rows(1).Delete
Selection.Columns(0).Value = CurrFile
Workbooks(CurrFile).Close False
Range("a1").Offset(Range("a1").CurrentRegion.Rows. Count - 1, 0).Select
ActiveCell.Delete
ActiveCell.Offset(0, 1).Select


NextFileNumber:
Next FileNumber

Range("a1").Select
Application.StatusBar = "Ready"

End Sub

What is the alternative way of fixing this?

Thanks for help!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Filesearch in 2007

This is no longer available in 2007.

'To loop through the files within a folder try the below code
Sub FileList()
Dim strFile As string, strFolder As string
strFolder = "c:\"

strFile = Dir(strFolder & "*.*", vbNormal)
Do While strFile < ""
MsgBox strFolder & strFile
strFile = Dir
Loop
End Sub

PS: You can use try FileSystemObject to loop through the files

--
Jacob


"GEORGIA" wrote:

Hi, I know little about VBA. Former employee wrote this code in 2003 but no
longer working in 2007 since 2007 dropped filesearch option.

Sub CombineFiles()

Application.DisplayAlerts = False
'On Error Resume Next

'declare variables
Dim FileCount As Long, FileNumber As Long, CurrFile As String
Dim myMacro As String, myNewFile As String, myFolder As String
Dim myFileRef As String

'assign values to variables
myMacro = ActiveWorkbook.Name
Application.Workbooks.Add
myNewFile = ActiveWorkbook.Name
Workbooks(myNewFile).Worksheets(1).Select
Range("a1").Select
myFileRef = Application.GetOpenFilename
Workbooks.Open Filename:=myFileRef
myFileRef = ActiveWorkbook.Name
myFolder = ActiveWorkbook.Path
ActiveWorkbook.Worksheets(1).Select
Range("a1").CurrentRegion.Rows(1).Copy
Workbooks(myNewFile).Activate
Range("b1").Select
ActiveSheet.Paste
Range("a1").Value = "File Name"
Range("b2").Select
Workbooks(myFileRef).Close False

'search for all Excel files in myFolder
With Application.FileSearch
.NewSearch
.LookIn = myFolder
.FileType = msoFileTypeExcelWorkbooks
.Execute
End With

'start loop to look inside each Excel file found
FileCount = Application.FileSearch.FoundFiles.Count
For FileNumber = 1 To FileCount
'give user status of macro while running
Application.StatusBar = "Searching " & FileNumber & " of " & FileCount &
" files."
'open file as read-only
Workbooks.Open Application.FileSearch.FoundFiles.Item(FileNumber) , ,
ReadOnly:=True
CurrFile = ActiveWorkbook.Name
Sheets(1).Select
Range("a1").Select
Selection.CurrentRegion.Copy
Workbooks(myNewFile).Activate
ActiveSheet.Paste
Selection.Rows(1).Delete
Selection.Columns(0).Value = CurrFile
Workbooks(CurrFile).Close False
Range("a1").Offset(Range("a1").CurrentRegion.Rows. Count - 1, 0).Select
ActiveCell.Delete
ActiveCell.Offset(0, 1).Select


NextFileNumber:
Next FileNumber

Range("a1").Select
Application.StatusBar = "Ready"

End Sub

What is the alternative way of fixing this?

Thanks for help!

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
FileSearch Excel 2007 shaz Excel Discussion (Misc queries) 4 March 21st 10 02:07 AM
Excel 2007 FileSearch James Price at Premier Excel Programming 3 June 29th 09 06:19 PM
ms excel 2007 Filesearch Danny[_2_] Excel Programming 2 May 12th 08 09:29 PM
Excel 2007 filesearch Libby Excel Programming 3 February 8th 08 06:53 PM
Application.FileSearch in 2007 Amery Excel Programming 2 December 13th 07 10:49 PM


All times are GMT +1. The time now is 03:51 PM.

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

About Us

"It's about Microsoft Excel"