Solved

Add existing worksheet in to a folder containing multiple workbooks

Posted on 2011-09-20
6
297 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
[X]
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
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…

752 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