Solved

Add existing worksheet in to a folder containing multiple workbooks

Posted on 2011-09-20
6
292 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
DevOps Toolchain Recommendations

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

 
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

Title # Comments Views Activity
AWS CLI issues with Tags 3 65
Sending Attachment via CDO 3 59
Create Excel formula on dynamic data 5 35
Excel Hyperlink Question 4 31
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

896 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

11 Experts available now in Live!

Get 1:1 Help Now