?
Solved

Opening and closing all excel files in a directory structure

Posted on 2009-04-28
5
Medium Priority
?
496 Views
Last Modified: 2012-05-06
Dineesh answered a question for me yesterday that allowed me to search through all excel files in a subfolder and replace a piece of text - this worked brilliantly.

What I could now do with though is a very similar piece of code to Dineesh's original one to open, save and close all excel files in a directory structure - including subdirectories - I am sure this is only a minor amendment but my vb isn't up to it I'm afraid (I'll put it on my to do list).

I have attached Dineesh's original code below - I don't need the search and replace in this second piece of code though.

thanks

Joy
Dim fso, folder, files, NewsFile,sFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
 
 Set fso = CreateObject("Scripting.FileSystemObject")
 sFolder = "C:\tests\two"
 Set folder = fso.GetFolder(sFolder)
 Set files = folder.Files
 
 For each folderIdx In files
if right(folderIdx,4) = ".xls" then
 
Set objWorkbook = objExcel.Workbooks.Open(folderIdx)
Set objWorksheet = objWorkbook.Worksheets(1)
 
Set objRange = objWorksheet.UsedRange
 
objRange.Replace "oldurl.com", "new url"
objWorkbook.Save
objWorkbook.close true
end if
Next

Open in new window

0
Comment
Question by:joypmor
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
TakedaT earned 2000 total points
ID: 24250609
Give this code a try.
Dim fso, folder, files, NewsFile,sFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
 
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "D:\test"
Set folder = fso.GetFolder(sFolder)
 
SearchDir folder,".xls"
 
Function SearchDir(strCurrentDir,strFileExt)
	For Each file In strCurrentDir.Files
		If right(file.name,4) = strFileExt then
			Set objWorkbook = objExcel.Workbooks.Open(file)
			Set objWorksheet = objWorkbook.Worksheets(1)
 			Set objRange = objWorksheet.UsedRange
 			objRange.Replace "oldurl.com", "new url"
			objWorkbook.Save
			objWorkbook.close true
		End If
	Next
	For Each folder In strCurrentDir.SubFolders
		SearchDir folder,strFileExt
	Next
End Function

Open in new window

0
 

Author Comment

by:joypmor
ID: 24252152
Thanks for this update - sorry I didn't respond earlier - I had been looking regularly and then couldn't for a while.

This works great, although I had to edit it slightly as I don't want to do the search and replace from this script, just the open, save, close (I know it seems a wierd thing to do but there is method in my madness honestly!)

Just one last question, can this be set to run from the current folder rather than hard-coding a start folder?

Thanks again

Joy
Dim fso, folder, files, NewsFile,sFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
 
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "C:\reporting\5_6 reports"
Set folder = fso.GetFolder(sFolder)
 
SearchDir folder,".xls"
 
Function SearchDir(strCurrentDir,strFileExt)
	For Each file In strCurrentDir.Files
		If right(file.name,4) = strFileExt then
			Set objWorkbook = objExcel.Workbooks.Open(file)
 			objWorkbook.Save
			objWorkbook.close true
		End If
	Next
	For Each folder In strCurrentDir.SubFolders
		SearchDir folder,strFileExt
	Next
End Function

Open in new window

0
 
LVL 10

Expert Comment

by:TakedaT
ID: 24252217
No prob.  I think this should do it.
Dim fso, folder, files, NewsFile,sFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
 
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set folder = fso.GetFolder(sFolder)
 
SearchDir folder,".xls"
 
Function SearchDir(strCurrentDir,strFileExt)
	For Each file In strCurrentDir.Files
		If right(file.name,4) = strFileExt then
			Set objWorkbook = objExcel.Workbooks.Open(file)
 			objWorkbook.Save
			objWorkbook.close true
		End If
	Next
	For Each folder In strCurrentDir.SubFolders
		SearchDir folder,strFileExt
	Next
End Function

Open in new window

0
 

Author Comment

by:joypmor
ID: 24254702
Hi

Thanks for getting back to me on this.  I'lll try it at work tomorrow because I had a problem running it on my laptop at home - for some reason tried to reinstall MS Office.

I'll let you know how it goes.

Joy
0
 

Author Closing Comment

by:joypmor
ID: 31575357
I'm so sorry, I thought I had already accepted this!

Works at treat thanks

Joy
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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