Excel Find and Replace across Multiple Text Files | VBA Macro #66

Share on facebook
Share on twitter
Share on linkedin

Macro Intro

What if you work on the interface team and are responsible for managing thousands of text files. After threats of malware your company is looking push security but there is a catch you are now responsible for updating all these .txt files with a new of code. Where do you even begin? This is a time consuming task and accuracy is critical which is why we created the Excel Find and Replace across Multiple Text Files macro! This is an Excel macro that will Find and Replace values in Multiple Text Files. This macro references 3 columns in Excel. In Column A are the values to Find. In the adjacent cell in column B is the value that will Replace what is found. Column C is optional allowing you an opportunity to populate the full path to any Excel Workbooks you want to search through. If nothing is populated in the Column C this macro will loop through all of the Excel Workbooks available in the Active Excel Workbook’s Folder Path. Once a Excel Workbook is opened this macro will loop through every Find and Replace value placed in Column A and Column B. This Excel macro is a great way to maintain Text Files that are routinely changing information.

See it in Action

Watch this video to see this macro in action.

***Video coming soon!***

Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Excel 16.0 Object Library, Microsoft Office 16.0 Object Library, OLE Automation
Copy the Code
'Leverage & Lean "Less Clicks, More Results"
Sub FindReplaceAcrossMultipleTextFilesFreeMacro()
' Means variable is in use
Dim FileCounter As Integer '
Dim FindReplaceCounter As Integer '
Dim FolderPath As String '
Dim LastRow As Integer '
Dim LastRowPath As Integer '
Dim oFile As Object '
Dim oFolder As Object '
Dim oFSO As Object '
Dim TextFile As Integer '
Dim TextFileContent As String '
Dim TextFilePath As String '
   
On Error GoTo LeverageLean
 
If Cells(2, 3).Value <> "" Then 'If a path to Text Files exist
FileCounter = 2
LastRowPath = Cells(Rows.Count, 3).End(xlUp).Row 'Identify Last Row in Column C
Do Until FileCounter > LastRowPath 'Loop through any Text Files in Column C
TextFilePath = Cells(FileCounter, 3).Value
TextFile = FreeFile 'The next file number available for use
Open TextFilePath For Input As TextFile 'Open the Text File in Read State
TextFileContent = Input(LOF(TextFile), TextFile) 'Store Text File Content in a String variable
Close TextFile 'Close Text File
FindReplaceCounter = 2
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Identify Last Row in Column A
Do Until FindReplaceCounter > LastRow 'Complete the Find and Replace for all values in Column A & B
TextFileContent = Replace(TextFileContent, Cells(FindReplaceCounter, 1).Value, Cells(FindReplaceCounter, 2).Value) 'Find/Replace
FindReplaceCounter = FindReplaceCounter + 1
Loop
TextFile = FreeFile 'The next file number available for use
Open TextFilePath For Output As TextFile 'Open the Text File in Write State
Print #TextFile, TextFileContent 'Write new content to Text File
Close TextFile 'Close Text File
FileCounter = FileCounter + 1
Loop
ElseIf Cells(2, 3).Value = "" Then 'If NO paths to Text Files exist
FolderPath = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.Name)) 'Active Excel Workbook File Path
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(FolderPath)
For Each oFile In oFolder.Files 'Loop through every File in Active Excel Workbook's folder path
If InStr(1, oFile.Type, "Text Document") <> 0 And InStr(1, oFile.Name, "~") = 0 Then 'If the File Type contains the phrase Text Document and is NOT Lock File
TextFilePath = FolderPath & oFile.Name
TextFile = FreeFile 'The next file number available for use
Open TextFilePath For Input As TextFile 'Open the Text File in Read State
TextFileContent = Input(LOF(TextFile), TextFile) 'Store Text File Content in a String variable
Close TextFile 'Close Text File
FindReplaceCounter = 2
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Identify Last Row in Column A
Do Until FindReplaceCounter > LastRow 'Complete the Find and Replace for all values in Column A & B
TextFileContent = Replace(TextFileContent, Cells(FindReplaceCounter, 1).Value, Cells(FindReplaceCounter, 2).Value) 'Find/Replace
FindReplaceCounter = FindReplaceCounter + 1
Loop
TextFile = FreeFile 'The next file number available for use
Open TextFilePath For Output As TextFile 'Open the Text File in Write State
Print #TextFile, TextFileContent 'Write new content to Text File
Close TextFile 'Close Text File
End If
Next oFile
End If
 
MsgBox "The Find and Replace has been completed. Stay Awesome!"
 
Set oFSO = Nothing
Set oFolder = Nothing
Set oFile = Nothing
  
Exit Sub
  
LeverageLean:
MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")
    
End Sub
'Stay Awesome

Insider Content

Here is the Insider code for this macro. This macro will allow you to use Column C to list multiple folder paths to loop through all of the Text Files found within them. This varies from the Free Macro behavior in that Column C is used to loop through individual Text Files which can be labor some if you have 100s of files to loop through in a handful of Folders. Column C will remain optional and if nothing is populated in Column C this macro will look to the same Folder Path as the Active Excel Workbook, find the Sub-Folders, and loop through each one finding Text Files to complete a Find and Replace. This macro will even loop through Sub-Folders within Sub-Folders. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Excel 16.0 Object Library, Microsoft Office 16.0 Object Library, OLE Automation

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Access, Excel, Outlook, PowerPoint, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Powered By MemberPress WooCommerce Plus Integration

Free Macro

Start creating Less Clicks, More Results today!