• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1178
  • Last Modified:

Need a batch file to update a number of excel files

I need help in creating a batch file that will find and replace a string of texts in a number of excel files that are located in a named folder. I am using Excel 2010 64bit and running a Windows 7 64 bit machine. Is a batch file even the best way to do this? If there is a another way other than a batch file I am open to that too.
1 Solution
Joe Winograd - EE Fellow & MVEDeveloperCommented:
This program's specs are exactly what you're looking for:

I ran across this program a while ago and am simply passing it along for your consideration. I have not tried it, so you're on your own to test and evaluate it. Note that it is not free...price is $19.99 USD and they use Digital River for distribution. Regards, Joe
QlemoBatchelor and DeveloperCommented:
If you need to do that often, applying some filtering criteria on the file names to process, maybe even some conditional replaces, then a batch file in PowerShell or VBScript is well suited. You could also use an Excel macro to change other workbooks' content. Neither of those approaches is complicated.

Maybe you should tell some more details, so we can get a better figure of what you are trying to accomplish.
Here is a macro that will replace text in all worksheets in all Excel files in a user-specified folder. List the text to replace in vFind. List the corresponding replacements in vReplace. The statements that need to change are commented in the code below, which should be installed in a regular module sheet.
Sub FolderTextReplacer()
Dim ws As Worksheet
Dim wb As Workbook
Dim flPath As String, flName As String
Dim vFind As Variant, vReplace As Variant
Dim i As Long, j As Long, n As Long
vFind = Array("Amalgamated Steelboom, Inc.", "Mammoth Enterprises, LLC")    'List as many strings to find as you like, separated by commas
vReplace = Array("Amalgamated Steelboom, LTD", "Mammoth Enterprises, Inc.") 'List corresponding replacement strings, separated by commas
flPath = Application.GetOpenFilename("Excel files (*.xls*), *xls*", _
        Title:="Pick any file in desired folder, then click 'Open'")
If flPath = "False" Then Exit Sub

n = UBound(vFind)
j = InStrRev(flPath, Application.PathSeparator)
flPath = Left(flPath, j)
flName = Dir(flPath & "*.xls*")

Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Do Until flName = ""
    Set wb = Workbooks(flName)
    If wb Is Nothing Then
        Set wb = Workbooks.Open(flPath & flName)
        For Each ws In wb.Worksheets
            For i = 0 To n
                ws.Cells.Replace vFind(i), vReplace(i), LookAt:=xlPart, MatchCase:=False
        wb.Close SaveChanges:=False
        Set wb = Nothing
    End If
    flName = Dir
On Error GoTo 0
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub

Open in new window

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

fdbguyAuthor Commented:
byundt, That worked like a charm...But what do you mean by "installed in a regular module sheet"? I have no experience in creating or running excel macros. Upon editing what you sent me it opens Visual Basic.
Macros can be placed in a regular module sheet, such as are named Module1 or Module2. This is what I did in the sample workbook I posted.

Macros can also be placed in the specialized class module sheets associated with each workbook, worksheet or chartsheet. These will have names like ThisWorkbook, Sheet1, Sheet2, Chart1, Chart2, etc. Although I could have put the macro there, good practice suggests not doing so.

Macros are written in Visual Basic for Applications, which is a subset of Visual Basic (i.e. it predates VB.Net). In the right hands, macros are a very powerful tool. Since the VBA language and IDE are furnished free of charge with every copy of Office, Microsoft should be applauded for giving power users the ability to do so much more than would seem possible to the casual observer.

When I post code as the possible solution to an Experts Exchange question, I give the briefest of instructions. Really, just enough to install the code and customize it to suit what I am guessing is the real problem. I'm glad to give more discussion when asked, but competitive pressures (often, the first person to post a working solution gets credit for the Answer) mean that any such discussion will happen after the formula or code has been found to solve the real problem.

Joe Winograd - EE Fellow & MVEDeveloperCommented:
Hi Brad,
That is an awesome solution and a terrific piece of code! Great stuff! Regards, Joe
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now