PC Review


Reply
Thread Tools Rate Thread

copy sheets from many files into 1

 
 
Steve
Guest
Posts: n/a
 
      8th Oct 2007
Hi everyone. I have a directory of excel files, each containing
multiple sheets (each may have different number of sheets). Can I
consolidate all sheets into 1 file? Basically copying each sheet from
each file and dropping each sheet into a single file. Thanks!

 
Reply With Quote
 
 
 
 
Bill Renaud
Guest
Posts: n/a
 
      9th Oct 2007
Hi Steve,

The following code was an answer to another poster to this same newsgroup
about 2 weeks ago with more or less the same problem. It displays an Open
dialog box so you can select all of the files you want to process. It then
successively opens each workbook. In the "ProcessAllFiles" routine
immediately below where the comment says "'Call your macro here.", insert a
call to a routine that will actually copy the data from each worksheet to
your single worksheet. (I don't have code to actually copy each worksheet,
as I don't know whether your data has headers in row 1, whether some rows
should be ignored, etc.)

Hopefully, this will give you a start.

'----------------------------------------------------------------------
Public Sub ProcessAllFiles()
Dim varFileList As Variant
Dim lngFileCount As Long
Dim ilngFileNumber As Long
Dim strFileName As String

'Index for varFileList starts at 1 in this situation,
'even without Option Base 1 statement at the top of the module.
varFileList = Application _
.GetOpenFilename(FileFilter:="Excel Files (*.xls),*.xls",
_
Title:="Open Excel File(s)", _
MultiSelect:=True)

lngFileCount = FileCount(varFileList)

If lngFileCount = 0 Then GoTo ExitSub
'User canceled out of dialog box.

For ilngFileNumber = 1 To lngFileCount
Workbooks.Open Filename:=CurrentFileName(varFileList, ilngFileNumber)

'Call your macro here.

'Set SaveChanges according to whether your macro already saved or not.
ActiveWorkbook.Close SaveChanges:=False
Next ilngFileNumber

ExitSub:
End Sub

'----------------------------------------------------------------------
Private Function FileCount(varFileList) As Long
Select Case VarType(varFileList)
Case vbBoolean
'User canceled out of the File Open dialog box.
FileCount = 0
Case vbString
'Dialog box is in single file mode.
'Single file selected for opening only.
FileCount = 1
Case vbArray + vbVariant
'Multiple files selected for processing.
FileCount = UBound(varFileList) - LBound(varFileList) + 1
End Select
End Function

'----------------------------------------------------------------------
Private Function CurrentFileName(varFileList As Variant, _
ilngFileNumber As Long) As String

Select Case VarType(varFileList)
Case vbBoolean
'User canceled out of the File Open dialog box.
CurrentFileName = ""
Case vbString
'Dialog box is in single file mode.
'Single file selected for opening only.
CurrentFileName = varFileList
Case vbArray + vbVariant
'Multiple files selected for processing.
'Return the filename currently pointed to.
CurrentFileName = CStr(varFileList(ilngFileNumber))
End Select
End Function

--
Regards,
Bill Renaud



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy two sheets into new files and save to multiple locations XP Microsoft Excel Programming 2 26th Nov 2008 02:46 PM
COPY SHEETS FROM TWO DIFFERENT EXCEL FILES INTO A THIRD Guy Cohen Microsoft Excel Programming 1 25th Mar 2007 05:09 PM
set up a macro to copy/ move sheets to individual files =?Utf-8?B?RGFuaWVsbGU0NA==?= Microsoft Excel Programming 1 20th Apr 2006 05:38 PM
Macro to copy sheets from several files into a new workbook. kiska1970@yahoo.com Microsoft Excel Programming 2 10th Nov 2005 10:45 PM
Copy text files into multiple sheets of 1 workbook Steve Microsoft Excel Programming 0 14th Jan 2004 08:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:53 PM.