Replacing text in a cell using VBA macro

FvM1985

New Member
Joined
Feb 20, 2018
Messages
7
Hi,

I have the following problem:

I am using a bigger VBA macro, as a part of this I want to search for a certain code(s) in a cell with multiple codes, and convert the entire cell with a new code/text. For example if a cell contains "NDM TRL WE5 YIC ZAH", I want to search for "TRL" and replace the entire cell with "Trend". And I have 6 other codes like "TRL" which will have to be replaced by other codes/texts..

I hope someone can help me.

This is what I have so far:
Range(Line).Select
ActiveCell.Formula = "=IF(ISNUMBER(SEARCH(""TRL"",$CF1,1)),""Trend"","
Range(Line).Select
Selection.Copy
Range(Range_Line).Select
ActiveSheet.Paste
 
After the text replacement, the macro has to continue with the following:

Range(BTO_VUV).Select
ActiveCell.Formula = "=IF(AND($AD1<>""Backorder CB"",$AD1<>""Backorder""),"""",IF(AND(OR($AD1=""Backorder CB"",$AD1=""Backorder""),(($L1-$AB1)<8)),""BTO"",""VUV""))"
Range(BTO_VUV).Select
Selection.Copy
Range(Range_BTO_VUV).Select
ActiveSheet.Paste

Etc.

I'm looking for a way to stop the part with the text replacement, after replacing the 15 codes.
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Assuming that BTO_VUV & Range_BTO_VUV are both named ranges try
Code:
Sub ReplaceText()

   Dim ary As Variant
   Dim Cnt As Long
   
   ary = Split("*TRL*|Trend|*ABC*|Start|*XYZ*|End", "|")
   With Sheets("Check")
      With .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         For Cnt = 0 To UBound(ary) Step 2
            .Replace ary(Cnt), ary(Cnt + 1), xlPart, , False, , False
         Next Cnt
      End With
   End With
   .Range("BTO_VUV").Formula = "=IF(AND($AD1<>""Backorder CB"",$AD1<>""Backorder""),"""",IF(AND(OR($AD1=""Backorder CB"",$AD1=""Backorder""),(($L1-$AB1)<8)),""BTO"",""VUV""))"
   .Range("BTO_VUV").Copy Range("Range_BTO_VUV")
End Sub
But I don't understand what you mean by
I'm looking for a way to stop the part with the text replacement, after replacing the 15 codes.
 
Upvote 0
After replacing the 15 codes in total, the macro keeps searching for another code and then I get the "subscript out of range" error.

Assuming that BTO_VUV & Range_BTO_VUV are both named ranges try
Code:
Sub ReplaceText()

   Dim ary As Variant
   Dim Cnt As Long
   
   ary = Split("*TRL*|Trend|*ABC*|Start|*XYZ*|End", "|")
   With Sheets("Check")
      With .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         For Cnt = 0 To UBound(ary) Step 2
            .Replace ary(Cnt), ary(Cnt + 1), xlPart, , False, , False
         Next Cnt
      End With
   End With
   .Range("BTO_VUV").Formula = "=IF(AND($AD1<>""Backorder CB"",$AD1<>""Backorder""),"""",IF(AND(OR($AD1=""Backorder CB"",$AD1=""Backorder""),(($L1-$AB1)<8)),""BTO"",""VUV""))"
   .Range("BTO_VUV").Copy Range("Range_BTO_VUV")
End Sub
But I don't understand what you mean by
 
Upvote 0
Could you please post your complete code, using code tags (the # icon in the reply window)
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,607
Members
449,174
Latest member
ExcelfromGermany

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