Search within text for multiple values and replace them with the values of another column

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Welcome. I have a topic that I think is difficult, but I have complete confidence that there is a solution that suits me on this wonderful forum full of experts. I am looking for a code or equation that enables me to search for a specific value within the text in column (A) and replace it with the value in column (F) to then be copied to the corresponding cell, column (G).
It is possible that there is more than one value that must be modified. It may reach 4 or 5 values. Often the words are somewhat similar. There are minor differences or an extra letter in some cases.
I have a series that can have 2000 rows or more

Capture d’écran 2024-07-11 152935.png


 

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.
With the help of some code written by @Peter_SSs, try this macro:
VBA Code:
Sub ReplaceText()
    Dim i As Long, ii As Long, v As Variant
    Dim RX As Object, itm As Object, s As String, V2 As Variant
    Const AccChars = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
    Const RegChars = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
    Dim A As String * 1
    Dim B As String * 1
    v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    For i = LBound(v) To UBound(v)
        For ii = 1 To Len(AccChars)
            A = Mid(AccChars, ii, 1)
            B = Mid(RegChars, ii, 1)
            If InStr(1, v(i, 1), A) Then
                Range("A" & i + 1) = Replace(Range("A" & i + 1), A, B)
            End If
        Next ii
    Next i
    Const Patt1 As String = "(^|\.|\!|\?)( *)([a-z])"
    Const Patt2 As String = "\bi\b"
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    V2 = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    For i = 1 To UBound(V2)
        s = LCase(V2(i, 1))
        RX.Pattern = Patt1
        For Each itm In RX.Execute(s)
          Mid(s, itm.firstindex + 1, itm.Length) = UCase(itm)
        Next itm
        RX.Pattern = Patt2
        s = RX.Replace(s, "I")
        V2(i, 1) = s
    Next i
    Range("A2").Resize(UBound(V2)).Value = V2
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
With the help of some code written by @Peter_SSs, try this macro:
VBA Code:
Sub ReplaceText()
    Dim i As Long, ii As Long, v As Variant
    Dim RX As Object, itm As Object, s As String, V2 As Variant
    Const AccChars = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
    Const RegChars = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
    Dim A As String * 1
    Dim B As String * 1
    v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    For i = LBound(v) To UBound(v)
        For ii = 1 To Len(AccChars)
            A = Mid(AccChars, ii, 1)
            B = Mid(RegChars, ii, 1)
            If InStr(1, v(i, 1), A) Then
                Range("A" & i + 1) = Replace(Range("A" & i + 1), A, B)
            End If
        Next ii
    Next i
    Const Patt1 As String = "(^|\.|\!|\?)( *)([a-z])"
    Const Patt2 As String = "\bi\b"
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    V2 = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
    For i = 1 To UBound(V2)
        s = LCase(V2(i, 1))
        RX.Pattern = Patt1
        For Each itm In RX.Execute(s)
          Mid(s, itm.firstindex + 1, itm.Length) = UCase(itm)
        Next itm
        RX.Pattern = Patt2
        s = RX.Replace(s, "I")
        V2(i, 1) = s
    Next i
    Range("A2").Resize(UBound(V2)).Value = V2
    Application.ScreenUpdating = True
End Sub

Unfortunately, I don't know how I can modify it so that it searches as previously mentioned in column A and replaces the values from column F and places them in column G
 
Upvote 0
Other than not placing the result in column F, is the result in column A correct?
 
Upvote 0
Perhaps I did not explain well. The values in column A do not change. They must remain the same. What is required is to search only in column A for the similar value in column F corresponding to each cell and replace it to get the sentences after replacing on column G.
 
Upvote 0
I understand that. When you run the macro, are the results in column A correct?
 
Upvote 0
I understand that. When you run the macro, are the results in column A correct?
It is not changed into incomprehensible symbols. I think it's because there are letters in Arabic, I think
 
Upvote 0
If the letters are in Arabic, I don’t think that I can help you. Sorry.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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