VBA to find and replace in text files

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
301
Office Version
  1. 365
Platform
  1. Windows
I need to loop through a folder full of xml files and find

<TaxCode>VIEXM</TaxCode>

and replace with

<TaxCode>VISTD</TaxCode>

Any pointers greatly appreciated.

Thank you for reading.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Just to add, I found this, I think it just needs a tweak
VBA Code:
Sub StringExistsInFile()

Dim theString As String

Dim theString2 As String

Dim path As String

Dim StrFile As String

Dim fso As New FileSystemObject

Dim file As TextStream

Dim line As String



theString = "KS12 KNW"

theString2 = "KS12KNW"

path = "\\aus-svv-sql15\ogi\exports archive\"

StrFile = Dir(path & "*.xml")



Do While StrFile <> ""



'Find TheString in the file

'If found, debug.print and exit loop



Set file = fso.OpenTextFile(path & StrFile)

Do While Not file.AtEndOfLine

line = file.ReadLine

If InStr(1, line, theString, vbTextCompare) > 0 Or InStr(1, line, theString2, vbTextCompare) > 0 Then

Debug.Print StrFile

Exit Do

End If

Loop



file.Close

Set file = Nothing

Set fso = Nothing



StrFile = Dir()

Loop

End Sub
 
Upvote 0
This is all you need. Only change to your folderpath

VBA Code:
Sub jec()
 Dim it
 With CreateObject("scripting.filesystemobject")
   For Each it In .getfolder("C:\Users\xxx\xxxx\").Files
      If .getextensionname(it) = "xml" Then .createtextfile(it).write Replace(.opentextfile(it).readall, "<TaxCode>VIEXM</TaxCode>", "<TaxCode>VISTD</TaxCode>")
   Next
 End With
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top