VBA To Replace Long Text String Using Look-Up Table

mjack19

New Member
Joined
Sep 27, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a spreadsheet where I want to replace the contents of a cell with the contents of another cell based on a table. I've written a VBA code that'll work if the replacement content is less than 255 characters, but gives me an error is the replacement text is longer than that. Can anyone help me with a solution? I've included screenshots of my workbook and the code I currently have. Thank you in advance for your help!

VBA Code:
Sub FindReplace()

For i = 2 To 500
    Worksheets("For Duping").Range("F:AE").Replace What:=Worksheets("Replacement Table").Cells(i, 1).Text, _
                                   Replacement:=Worksheets("Replacement Table").Cells(i, 2).Text, LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, MatchCase:=False
Next i

End Sub
 

Attachments

  • For Duping.PNG
    For Duping.PNG
    40.6 KB · Views: 14
  • Replacement Table.PNG
    Replacement Table.PNG
    60.6 KB · Views: 15
  • Desired Outcome.PNG
    Desired Outcome.PNG
    52.8 KB · Views: 14

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Please try this

VBA Code:
Sub FindReplace()
  Dim Rng As Range
  Dim Cel As Range
  Dim LUCol As Range
  Dim LUVal As String
  Dim RetVal As String
  
  Set Rng = Worksheets("For Duping").Range("F:AE")
  Set LUCol = Worksheets("Replacement Table").Range("A2:A501")
  
  For Each Cel In LUCol
    LUVal = Cel.Value
    RetVal = Cel.Offset(0, 1).Value
    
    Rng.Replace What:=LUVal, Replacement:=RetVal, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
  Next Cel
 
Upvote 0
Sorry I misunderstood the 255 character limitation. Try this. It will be much slower

VBA Code:
Sub FindReplace()
  Dim Rng As Range
  Dim Cel As Range
  Dim LUcol As Range
  Dim LUVal As String
  Dim LUCel As Range
  
  Set Rng = Worksheets("For Duping").Range("F:AE")
  Set LUcol = Worksheets("Replacement Table").Range("A2:A501")
  
  Application.Calculation = xlCalculationManual
  
  For Each Cel In Rng
    If Cel.Value <> "" Then
      For Each LUCel In LUcol
        If LUCel.Value <> "" Then
          DoEvents
          If LUCel.Value = Cel.Value Then
            Cel.Value = LUCel.Offset(0, 1).Value
            Exit For
          End If
        End If
      Next LUCel
    End If
  Next Cel
  
  
  Application.Calculation = xlCalculationAutomatic
  
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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