VBA - loop through text files then find & replace

HotLanta

Board Regular
Joined
Nov 3, 2005
Messages
176
Hi,

I wanted to see if someone could help with this project. I have many text files that I am always manually editing.


I would like to have a macro that will loop through a folder on the desktop named "Text Files".
In the folder will be any number of text files. (.txt)
I want the code to find a certain piece of information and then use that to replace another piece of information.
Then save the file and loop through the rest.

The text files look like this:

FILNAM/'SAMPLE TEST PROTOTYPE'
DATE =2022/10/14
TIME =11:00:43

PR(1)='98765432'
LI(2)='EFG'
PL(3)='TUVWXY'
PN(4)='COMPANY'
PS(5)='12345678-001'
Q(6)='ABCD'

I would like for the PS(5) line to be found and then the first 8 characters copied that are in single quotes.

Then find the FILENAM/ line and replace whatever is between the single quotes with what is copied in the clipboard.

So the final result will look like this:

FILNAM/'12345678'
DATE =2022/10/14
TIME =11:00:43

PR(1)='98765432'
LI(2)='EFG'
PL(3)='TUVWXY'
PN(4)='COMPANY'
PS(5)='12345678-001'
Q(6)='ABCD'

Then loop through the rest.

Is this possible?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I wasn't sure what Power Automate was, so I had to look it up. It seems much bigger than this project and I am sure my company won't go for it.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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