Solved

Add existing worksheet in to a folder containing multiple workbooks

Posted on 2011-09-20
6
291 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now