Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 312
Default Open all files, ignore some

Hi. The below code opens all files within a given folder, and consolidates
them into 1 file, 1 sheet. In each of the files to be consolidated, I have
a veryhidden sheet called Test. Is there a way to have the code open files
within the folder and consolidate ONLY the files that contain the sheet
Test? These files are templates that are being emailed in. I just want to
make sure that the file I get back is my file, so the formats are exactly
the same. Also, it would be great to write a log to a text file that
detailed the names of the files that were not consolidated becasue they did
not contain the sheet Test. Thanks in advance for any and all of your help

Dim sFolder As String
Dim wb As Workbook
Dim i As Long
Dim fname As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Consol").Select
With Application.FileSearch
.NewSearch
.LookIn = "\\server\folder1\folder2\"
.SearchSubFolders = False
.filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(filename:=.FoundFiles(i))
fname = Left(ActiveWorkbook.name, Len(ActiveWorkbook.name) - 4)
wb.ActiveSheet.Range("G5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Valu e = fname
wb.ActiveSheet.Range("A5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("Consol").Range("A" & _

ThisWorkbook.Worksheets("Consol").Range("D65536"). End(xlUp).Offset(1,
0).Row).PasteSpecial _
Paste:=xlPasteValues
wb.Close savechanges:=False
fname = Nothing
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Open all files, ignore some

Hi Steph

Normal you must open the file to check the sheet name but you can use this
to check it also without opening the file.

On Error Resume Next
SheetCheck = ExecuteExcel4Macro(PathStr & Range("A1").Address(, , xlR1C1))
If Err.Number < 0 Then
'other code

http://www.rondebruin.nl/summary2.htm
See how i use it here


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Steph" wrote in message ...
Hi. The below code opens all files within a given folder, and consolidates
them into 1 file, 1 sheet. In each of the files to be consolidated, I have
a veryhidden sheet called Test. Is there a way to have the code open files
within the folder and consolidate ONLY the files that contain the sheet
Test? These files are templates that are being emailed in. I just want to
make sure that the file I get back is my file, so the formats are exactly
the same. Also, it would be great to write a log to a text file that
detailed the names of the files that were not consolidated becasue they did
not contain the sheet Test. Thanks in advance for any and all of your help

Dim sFolder As String
Dim wb As Workbook
Dim i As Long
Dim fname As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Consol").Select
With Application.FileSearch
.NewSearch
.LookIn = "\\server\folder1\folder2\"
.SearchSubFolders = False
.filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(filename:=.FoundFiles(i))
fname = Left(ActiveWorkbook.name, Len(ActiveWorkbook.name) - 4)
wb.ActiveSheet.Range("G5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Valu e = fname
wb.ActiveSheet.Range("A5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("Consol").Range("A" & _

ThisWorkbook.Worksheets("Consol").Range("D65536"). End(xlUp).Offset(1,
0).Row).PasteSpecial _
Paste:=xlPasteValues
wb.Close savechanges:=False
fname = Nothing
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Open all files, ignore some

Steph,

here is a revised version. I have included a function to test if a
sheetexists.

BTW, you had a line
fname = Nothing
which errors for me as fname ius a string, so I change it to
fname = ""

Dim sFolder As String
Dim wb As Workbook
Dim i As Long
Dim fname As String
Dim FileNumber As Long

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Consol").Select
FileNumber = FreeFile ' Get unused file
' number.
Open "c:\Findlog" For Output As #FileNumber
With Application.FileSearch
.NewSearch
.LookIn = "\\server\folder1\folder2\"
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(Filename:=.FoundFiles(i))
If SheetExists("Test", wb) Then
fname = Left(ActiveWorkbook.Name,
Len(ActiveWorkbook.Name) - 4)
wb.ActiveSheet.Range("G5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).row).Valu e =
fname
wb.ActiveSheet.Range("A5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).row).Copy
ThisWorkbook.Worksheets("Consol").Range("A" & _

ThisWorkbook.Worksheets("Consol").Range("D65536"). End(xlUp).Offset(1,
0).row).PasteSpecial _
Paste:=xlPasteValues
wb.Close savechanges:=False
fname = ""
Else
Write #FileNumber, wb.FullName
End If
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With
Close #FileNumber
Application.ScreenUpdating = True
Application.DisplayAlerts = True





'-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function







--

HTH

RP
(remove nothere from the email address if mailing direct)


"Steph" wrote in message
...
Hi. The below code opens all files within a given folder, and

consolidates
them into 1 file, 1 sheet. In each of the files to be consolidated, I

have
a veryhidden sheet called Test. Is there a way to have the code open

files
within the folder and consolidate ONLY the files that contain the sheet
Test? These files are templates that are being emailed in. I just want

to
make sure that the file I get back is my file, so the formats are exactly
the same. Also, it would be great to write a log to a text file that
detailed the names of the files that were not consolidated becasue they

did
not contain the sheet Test. Thanks in advance for any and all of your

help

Dim sFolder As String
Dim wb As Workbook
Dim i As Long
Dim fname As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Consol").Select
With Application.FileSearch
.NewSearch
.LookIn = "\\server\folder1\folder2\"
.SearchSubFolders = False
.filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(filename:=.FoundFiles(i))
fname = Left(ActiveWorkbook.name, Len(ActiveWorkbook.name) -

4)
wb.ActiveSheet.Range("G5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Valu e = fname
wb.ActiveSheet.Range("A5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("Consol").Range("A" & _

ThisWorkbook.Worksheets("Consol").Range("D65536"). End(xlUp).Offset(1,
0).Row).PasteSpecial _
Paste:=xlPasteValues
wb.Close savechanges:=False
fname = Nothing
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 312
Default Open all files, ignore some

Thank you so much!

"Bob Phillips" wrote in message
...
Steph,

here is a revised version. I have included a function to test if a
sheetexists.

BTW, you had a line
fname = Nothing
which errors for me as fname ius a string, so I change it to
fname = ""

Dim sFolder As String
Dim wb As Workbook
Dim i As Long
Dim fname As String
Dim FileNumber As Long

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Consol").Select
FileNumber = FreeFile ' Get unused file
' number.
Open "c:\Findlog" For Output As #FileNumber
With Application.FileSearch
.NewSearch
.LookIn = "\\server\folder1\folder2\"
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(Filename:=.FoundFiles(i))
If SheetExists("Test", wb) Then
fname = Left(ActiveWorkbook.Name,
Len(ActiveWorkbook.Name) - 4)
wb.ActiveSheet.Range("G5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).row).Valu e =
fname
wb.ActiveSheet.Range("A5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).row).Copy
ThisWorkbook.Worksheets("Consol").Range("A" & _

ThisWorkbook.Worksheets("Consol").Range("D65536"). End(xlUp).Offset(1,
0).row).PasteSpecial _
Paste:=xlPasteValues
wb.Close savechanges:=False
fname = ""
Else
Write #FileNumber, wb.FullName
End If
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With
Close #FileNumber
Application.ScreenUpdating = True
Application.DisplayAlerts = True





'-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function







--

HTH

RP
(remove nothere from the email address if mailing direct)


"Steph" wrote in message
...
Hi. The below code opens all files within a given folder, and

consolidates
them into 1 file, 1 sheet. In each of the files to be consolidated, I

have
a veryhidden sheet called Test. Is there a way to have the code open

files
within the folder and consolidate ONLY the files that contain the sheet
Test? These files are templates that are being emailed in. I just want

to
make sure that the file I get back is my file, so the formats are

exactly
the same. Also, it would be great to write a log to a text file that
detailed the names of the files that were not consolidated becasue they

did
not contain the sheet Test. Thanks in advance for any and all of your

help

Dim sFolder As String
Dim wb As Workbook
Dim i As Long
Dim fname As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets("Consol").Select
With Application.FileSearch
.NewSearch
.LookIn = "\\server\folder1\folder2\"
.SearchSubFolders = False
.filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(filename:=.FoundFiles(i))
fname = Left(ActiveWorkbook.name, Len(ActiveWorkbook.name) -

4)
wb.ActiveSheet.Range("G5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Valu e = fname
wb.ActiveSheet.Range("A5:G" & _
wb.ActiveSheet.Range("D65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("Consol").Range("A" & _

ThisWorkbook.Worksheets("Consol").Range("D65536"). End(xlUp).Offset(1,
0).Row).PasteSpecial _
Paste:=xlPasteValues
wb.Close savechanges:=False
fname = Nothing
Next i
Else
MsgBox "Folder " & sFolder & " contains no required

files"
End If
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True






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
emailing files from excel, the files will not go until I open up . joe New Users to Excel 2 September 18th 09 02:12 PM
how do I toggle between 2 open excel files and leave both open Big D in Brighton Excel Discussion (Misc queries) 1 November 6th 08 04:28 PM
How to change default Open/Files of Type to "Microsoft Excel Files Tammy Excel Discussion (Misc queries) 2 January 14th 08 11:06 PM
I cant open files unless I open the Excel program first ElaineAng Excel Discussion (Misc queries) 3 February 16th 06 02:54 PM
file open via IE hyperlink causes already open files to shrink and tile Marc Setting up and Configuration of Excel 0 May 4th 05 08:13 PM


All times are GMT +1. The time now is 05:54 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"