Solved

Add existing worksheet in to a folder containing multiple workbooks

Posted on 2011-09-20
6
296 Views
Last Modified: 2012-05-12
Hi,

I am looking for a script that will add a existing workbook as a new tab to a multitude of existing workbooks. The existing workbooks are based of a template although they are saved under different names and some are in sub folders. Is the best way to do this via a script or a macro? I attempted to look at other solutions but I was unable to find a solution specific to this problem,

Basically, this new worksheet needs to be added to the excel workbooks contained in the folder and subfolders as a new tab. Does anyone have a suggestion on the best way to do this? Thanks in advance.
0
Comment
Question by:Mattywerts
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:Scottyworld
ID: 36571201
I've done this before but can't find the exact script at the moment, but basically...

Create a vbscript that recurses through every excel file in the folder/sub-folders you specify
Grab the filename
Open the spreadsheet
Add a new worksheet
Save and close the file
Move onto the next one
0
 
LVL 1

Author Comment

by:Mattywerts
ID: 36571219
Scottyworld,

Thanks for that suggestion. That is exactly what I am looking for will see what I can find.
0
 
LVL 9

Accepted Solution

by:
Scottyworld earned 500 total points
ID: 36571243
Hi ya,
Could find my existing script so have quickly wrote another..... might want to test it first, but it should do exactly what you need. Save the following as a .vbs file
Modify line 2 to reflect the folder you want to start in. It will then add a worksheet to ALL xls and xlsx files below this folder, including sub folders
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\temp\test"

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    strFile = objFile.Path
	If Right(strFile,4) = "xlsx" or Right(strFile,3) = "xls" then
		InsertWorksheet
	End If
Next

ParseSubfolders objFSO.GetFolder(objStartFolder)

Sub ParseSubfolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
			strFile = objFile.Path
			If Right(strFile,4) = "xlsx" or Right(strFile,3) = "xls" then
				InsertWorksheet
			End If
        Next
        ParseSubfolders Subfolder
    Next
End Sub

Sub InsertWorksheet
	Set objExcel = CreateObject("Excel.Application")
	Set objWorkbook = objExcel.Workbooks.Open(strFile)
	set objWorksheet = objExcel.Sheets.Add( , objExcel.WorkSheets(objExcel.WorkSheets.Count))
	objExcel.ActiveWorkbook.Save 
	objExcel.ActiveWorkbook.Close
End Sub

wscript.echo "Finished"

Open in new window

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 1

Author Comment

by:Mattywerts
ID: 36577801
Thanks heaps for that. Do you know if it is possible to add a existing worksheet to workbooks via this  script?
0
 
LVL 1

Author Closing Comment

by:Mattywerts
ID: 36578067
This was of a great help. Thanks
0
 
LVL 9

Expert Comment

by:Scottyworld
ID: 36578239
A slight modification of the script will enable you to do that, or you could use one that Rob has already posted on EE

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23149549.html
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question