Find and Replace Text in Multiple Columns with Data from Two Other Column

Bosca

New Member
Joined
Feb 11, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have been trying to write VBA code that will replace text XXX with the text in cell A2, then A3, A4, etc. Also then replacing the text YYY with text from B2, then B3, B4, etc. across the range of cells from C to H.

I have a large amount of data so it will take a long time to do this over and over again.

TypeSerial numberReplacement Range 1Replacement Range 2Replacement Range 3Replacement Range 4Replacement Range 5Replacement Range 6
Boeing
1111​
XXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_Excel
Airbus
2222​
XXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_Excel
Embraer
3333​
XXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_ExcelXXX_YYY_Excel

Can someone assist with VBA code for this?

Thanks!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Does this do what you want...
VBA Code:
Sub Bosca()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("C2:H" & LastRow) = Evaluate(Replace("SUBSTITUTE(SUBSTITUTE(C2:H#,""XXX"",A2:A#),""YYY"",B2:B#)", "#", LastRow))
End Sub
 
Upvote 0
Does this do what you want...
VBA Code:
Sub Bosca()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("C2:H" & LastRow) = Evaluate(Replace("SUBSTITUTE(SUBSTITUTE(C2:H#,""XXX"",A2:A#),""YYY"",B2:B#)", "#", LastRow))
End Sub

Thanks Rick, hasn't exactly worked. I left out the directory of the file name (didn't think it would matter here), but using formula text and then the VBA above it does what I want it to do.

Without the formulatext function it doesn't work exactly in my use case. It is running now and taking a while to run but this is because I'm accessing a direct link to over 300 other excel files.

Thanks for the above!
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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