Advertisement

10.04.2005 at 01:33PM PDT, ID: 21583783
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

VBA - Open multiple workbooks and copy sheet to new workbook
Tags: vba, open, workbook, copy
Hey all, I have been assigned a new project that will require a little VBA, and my VBA skills are very elementary.

I have 100 files (Book1.xls through Book100.xls)
All files reside in C:\Temp

I need to open each workbook, copy Sheet1, paste to next available Sheet# in Summary.xls, close the workbook and move to the next.

Thanks in advance for suggestions.

Bud
Start your free trial to view this solution
Question Stats
Zone: Software
Question Asked By: BTognietti
Solution Provided By: matthewspatrick
Participating Experts: 2
Solution Grade: A
Views: 1182
Translate:
Loading Advertisement...
10.04.2005 at 01:43PM PDT, ID: 15017590

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2005 at 01:51PM PDT, ID: 15017669

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2005 at 02:23PM PDT, ID: 15017891

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2005 at 02:26PM PDT, ID: 15017900

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2005 at 02:27PM PDT, ID: 15017907

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2005 at 02:27PM PDT, ID: 15017914

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2005 at 02:36PM PDT, ID: 15017984

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.04.2005 at 02:38PM PDT, ID: 15017999

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
10.04.2005 at 01:43PM PDT, ID: 15017590

Rank: Genius

Hi Bud,

Try this.  It assumes that none of the 100 workbooks are open, and that it will save a Summary.xls to the same directory:

Sub Summarize()

    Dim Counter As Long
    Dim Source As Workbook
    Dim Dest As Workbook

    Const MyDir As String = "c:\temp\"

    Application.ScreenUpdating = False

    For Counter = 1 To 100
        Set Source = Workbooks.Open(MyDir & "Book" & Counter & ".xls")
        If Counter = 1 Then
            Source.Worksheets("Sheet1").Copy
            Set Dest = ActiveWorkbook
        Else
            Source.Worksheets("Sheet1").Copy After:=Dest.Worksheets(Dest.Worksheets.Count)
        End If
        Source.Close False
    Next

    Dest.SaveAs MyDir & "Summary.xls"

    Application.ScreenUpdating = True

    MsgBox "Done"

End Sub


Regards,

Patrick
 
10.04.2005 at 01:51PM PDT, ID: 15017669

Rank: Genius

Hi Bud,

Though Patrick has what I think you're looking for, you could also use something like this to combine all the data into as few sheets as necessary.  Of course this assumes your data all has the same format, but will work regardless:

Option Explicit
Sub CombineSheetsFromAllFilesInADirectory()
 
 Dim Path      As String 'string variable to hold the path to look through
 Dim FileName  As String 'temporary filename string variable
 Dim tWB       As Workbook 'temporary workbook (each in directory)
 Dim tWS       As Worksheet 'temporary worksheet variable
 Dim mWB       As Workbook 'master workbook
 Dim aWS       As Worksheet 'active sheet in master workbook
 Dim RowCount  As Long 'Rows used on master sheet
 Dim uRange    As Range 'usedrange for each temporary sheet
 Dim LastColm  As Range 'Range variable, will be used to find the last used column
 
  '***** Set folder to cycle through *****
 Path = "C:\temp\" 'Change as needed
 
 Application.EnableEvents = False 'turn off events
 Application.ScreenUpdating = False 'turn off screen updating
 Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
 Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
 If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
  Path = Path & Application.PathSeparator 'add "\"
 End If
 FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
 Do Until FileName = "" 'loop until all files have been parsed
  If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
   Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
   For Each tWS In tWB.Worksheets 'loop through each sheet
    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
     aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
     Set LastColm = aWS.Cells.Find(What:="*", After:=Range("IV1"), _
      SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
     If LastColm.Column <> 255 Then
      aWS.Range(aWS.Columns(LastColm.Column + 1), aWS.Columns(255)).Delete
     End If
     RowCount = aWS.UsedRange.Rows.Count 'Reset scroll bars and last cell
     Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
     RowCount = 0 'reset RowCount variable
    End If
    If RowCount = 0 Then 'if working with a new sheet
     aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
     tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
     aWS.Range("IV1").Value = "Source Sheet"
     RowCount = 1 'add one to rowcount
    End If
    With aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count)
     .Value = uRange.Value 'move data from temp sheet to data sheet
     Intersect(.EntireRow, aWS.Columns("IV")).Value = tWS.Name
    End With
    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
   Next 'tWS
   tWB.Close False 'close temporary workbook without saving
  End If
  FileName = Dir() 'set next file's name to FileName variable
 Loop
 aWS.Columns.AutoFit 'autofit columns on last data sheet
 Set LastColm = aWS.Cells.Find(What:="*", After:=Range("IV1"), _
  SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
 If LastColm.Column <> 255 Then
  aWS.Range(aWS.Columns(LastColm.Column + 1), aWS.Columns(255)).Delete
 End If
 RowCount = aWS.UsedRange.Rows.Count 'Reset scroll bars and last cell
 mWB.Sheets(1).Select 'select first data sheet on master workbook
 Application.EnableEvents = True 're-enable events
 Application.ScreenUpdating = True 'turn screen updating back on
 
  'Clear memory of the object variables
 Set tWB = Nothing
 Set tWS = Nothing
 Set mWB = Nothing
 Set aWS = Nothing
 Set uRange = Nothing
 Set LastColm = Nothing
End Sub

Matt
 
10.04.2005 at 02:23PM PDT, ID: 15017891
Thanks for the quick responses,

Matt,

I will eventually need to combine the sheets into one master, but the sheets have a very funky format to them right now, so I am starting by importing them all to new sheets in one workbook, then extract specific values to a master sheet. Thanks for the effort though!

Patrick,

That worked great! Only one question, it named the Sheets Sheet1, Sheet1 (2), Sheet1 (3), Etc.

How can I import as just Sheet1, Sheet2, Sheet3? Or even better, 1, 2, 3, Etc.

Thanks!

Bud
 
10.04.2005 at 02:26PM PDT, ID: 15017900

Rank: Genius

(still give patrick the points for this q)

At the end of his sub (before the done msgbox), put
 For Counter = 1 to Worksheets.Count
  Worksheets(Counter).Name = Counter
 Next
 
10.04.2005 at 02:27PM PDT, ID: 15017907

Rank: Genius

Bud,

Easily accomplished :)

Sub Summarize()

    Dim Counter As Long
    Dim Source As Workbook
    Dim Dest As Workbook

    Const MyDir As String = "c:\temp\"

    Application.ScreenUpdating = False

    For Counter = 1 To 100
        Set Source = Workbooks.Open(MyDir & "Book" & Counter & ".xls")
        If Counter = 1 Then
            Source.Worksheets("Sheet1").Copy
            Set Dest = ActiveWorkbook
            ActiveSheet.Name = Counter
        Else
            Source.Worksheets("Sheet1").Copy After:=Dest.Worksheets(Dest.Worksheets.Count)
            Dest.Worksheets(Dest.Worksheets.Count).Name = Counter
        End If
        Source.Close False
    Next

    Dest.SaveAs MyDir & "Summary.xls"

    Application.ScreenUpdating = True

    MsgBox "Done"

End Sub
Accepted Solution
 
10.04.2005 at 02:27PM PDT, ID: 15017914

Rank: Genius

I understand, btw, about the combining of the sheets.. You may want to try it anyways to see how it would work, as it may save a little time.  The routine is one I wrote for something else, so don't feel bad if you don't even try it.  I just wanted to put it here, as it could work for you if certain circumstances existed
 
10.04.2005 at 02:36PM PDT, ID: 15017984
Matt,

That is actually very cool and I can already think of a bunch of uses for it  ;-)

Patrick,

That did it. Thanks!
 
10.04.2005 at 02:38PM PDT, ID: 15017999

Rank: Genius

Bud,

You're most welcome.

Patrick
 
 
20080236-EE-VQP-29