Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add existing worksheet in to a folder containing multiple workbooks

Posted on 2011-09-20
6
295 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 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