Solved

Need a batch file to update a number of excel files

Posted on 2013-12-14
6
596 Views
Last Modified: 2013-12-15
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.
0
Comment
Question by:fdbguy
6 Comments
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 39718876
This program's specs are exactly what you're looking for:
http://www.sobolsoft.com/excelreplace/

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
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39718957
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.
0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39719450
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
            Next
        Next
        wb.Save
        wb.Close SaveChanges:=False
        Set wb = Nothing
    End If
    flName = Dir
Loop
On Error GoTo 0
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub

Open in new window

ReplaceTextAllFilesInFolderQ2831.xlsm
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:fdbguy
ID: 39719970
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.
0
 
LVL 80

Expert Comment

by:byundt
ID: 39720004
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.

Brad
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 39720050
Hi Brad,
That is an awesome solution and a terrific piece of code! Great stuff! Regards, Joe
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

743 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

12 Experts available now in Live!

Get 1:1 Help Now