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 question

Hi. I found some code on the newsgroup that I have a question about. This
code opens all files within a particular folder. I have a bunch of files I
wish to open, copy the contents of the ONLY sheet in the file, and paste to
the file that the code resides in. The problem is each file in the folder
has a differently names sheet. So I modified the code to grab data from the
ActiveSheet, but that errors out - I think becasue the sheet is not active.
How do I get around this?

Sub OpenAllFiles()
Dim sFolder As String
Dim wb As Workbook
Dim i As Long

With Application.FileSearch
.NewSearch
.LookIn = "C:\Test" 'Folder Location on server
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(Filename:=.FoundFiles(i))

wb.ActiveSheet.Range("A5:AG" & _
.Range("G20").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("GM_Approval").Range("A" & _
.Range("G65536").End(xlUp).Offset(1, 0).Row).PasteSpecial _
Paste:=xlPasteValues

wb.Close SaveChanges:=False
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Open all files question

Only one worksheet???

Then I'd just look at worksheets(1):


With wb.Worksheets(1)
.Range("A5:AG" & .Range("G20").End(xlUp).Row).Copy
End With

'and be careful with the paste, too

With ThisWorkbook.Worksheets("GM_Approval")
.Range("A" & .Range("G65536").End(xlUp).Offset(1, 0).Row).PasteSpecial _
Paste:=xlPasteValues
End With

I didn't test this, but you want to be a little careful.

..Range(...)
refers to the previous With object.

In your code, the previous object was: Application.FileSearch





Steph wrote:

Hi. I found some code on the newsgroup that I have a question about. This
code opens all files within a particular folder. I have a bunch of files I
wish to open, copy the contents of the ONLY sheet in the file, and paste to
the file that the code resides in. The problem is each file in the folder
has a differently names sheet. So I modified the code to grab data from the
ActiveSheet, but that errors out - I think becasue the sheet is not active.
How do I get around this?

Sub OpenAllFiles()
Dim sFolder As String
Dim wb As Workbook
Dim i As Long

With Application.FileSearch
.NewSearch
.LookIn = "C:\Test" 'Folder Location on server
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(Filename:=.FoundFiles(i))

wb.ActiveSheet.Range("A5:AG" & _
.Range("G20").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("GM_Approval").Range("A" & _
.Range("G65536").End(xlUp).Offset(1, 0).Row).PasteSpecial _
Paste:=xlPasteValues

wb.Close SaveChanges:=False
Next i
Else
MsgBox "Folder " & sFolder & " contains no required files"
End If
End With

End Sub


--

Dave Peterson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 312
Default Open all files question

Perfect. Thanks for the info!

"Dave Peterson" wrote in message
...
Only one worksheet???

Then I'd just look at worksheets(1):


With wb.Worksheets(1)
.Range("A5:AG" & .Range("G20").End(xlUp).Row).Copy
End With

'and be careful with the paste, too

With ThisWorkbook.Worksheets("GM_Approval")
.Range("A" & .Range("G65536").End(xlUp).Offset(1,
0).Row).PasteSpecial _
Paste:=xlPasteValues
End With

I didn't test this, but you want to be a little careful.

.Range(...)
refers to the previous With object.

In your code, the previous object was: Application.FileSearch





Steph wrote:

Hi. I found some code on the newsgroup that I have a question about.
This
code opens all files within a particular folder. I have a bunch of files
I
wish to open, copy the contents of the ONLY sheet in the file, and paste
to
the file that the code resides in. The problem is each file in the
folder
has a differently names sheet. So I modified the code to grab data from
the
ActiveSheet, but that errors out - I think becasue the sheet is not
active.
How do I get around this?

Sub OpenAllFiles()
Dim sFolder As String
Dim wb As Workbook
Dim i As Long

With Application.FileSearch
.NewSearch
.LookIn = "C:\Test" 'Folder Location on server
.SearchSubFolders = False
.Filename = "*.xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wb = Workbooks.Open(Filename:=.FoundFiles(i))

wb.ActiveSheet.Range("A5:AG" & _
.Range("G20").End(xlUp).Row).Copy
ThisWorkbook.Worksheets("GM_Approval").Range("A" & _
.Range("G65536").End(xlUp).Offset(1, 0).Row).PasteSpecial _
Paste:=xlPasteValues

wb.Close SaveChanges:=False
Next i
Else
MsgBox "Folder " & sFolder & " contains no required
files"
End If
End With

End Sub


--

Dave Peterson



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
Can not open excel files without open application Taarfa Excel Discussion (Misc queries) 3 July 9th 05 11:56 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 11:25 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"