在Excel中查找/替换表中的多个子字符串

时间:2016-03-02 10:10:07

标签: excel vba

我将文本复制/粘贴到Excel中的单元格中,该单元格具有需要替换的特定子字符串,从~4,000行的列表中删除。我有相同数量的文本/内容作为行数,我需要手动复制/粘贴。

基本上,特定的子字符串需要用相同的子字符串替换,用HTML链接包装,但这需要在我的列表中出现的所有子字符串中发生。

实施例

我有这样的文字:

Microsoft Excel is a spreadsheet developed by Microsoft for Windows, Mac OS X, and iOS.

在表#2中,我有以下内容;

Example

然后我需要excel输出上面的文本,指定的字符串替换为以下格式的链接:

<a href="http://www.apple.com/uk/mac/">Mac</a>

在此示例中,它将输出以下内容:

<a href="https://www.microsoft.com/">Microsoft</a> Excel is a spreadsheet developed by <a href="https://www.microsoft.com/">Microsoft</a> for <a href="https://www.microsoft.com/en-gb/windows">Windows </a>, <a href="http://www.apple.com/uk/mac/">Mac</a> OS X, and iOS.

我不是一位优秀的专家,所以我找不到解决方案。关于SO的一个答案提出了以下一个不同的问题; =SUBSTITUTE(A2,"Author","Authoring") 但我不知道如何编辑它以包含所有字符串而无需手动将每一个字符串添加到公式中,因为这将只是逐个替换每个字符串。

之前有没有人做过类似的事情?如果是这样,你是怎么做到的?

1 个答案:

答案 0 :(得分:0)

在这种情况下,您面临的最大问题是误报。当在另一个单词或短语中找到一个单词或短语时,您可能最终会“双重处理”或错误地处理搜索词。为避免这种情况,请按照长度的降序处理每个术语,并分两步进行。首先,用绝对唯一的临时字符串替换搜索词,一旦所有术语都被重新分配,请返回并用实际的HTML锚元素替换临时字符串。

Module1代码表

Option Explicit

Public Const csANCHOR As String = "<a href=""×LL×"">×FN×</a>"

Sub processBlurbs()
    Dim m As Long, w As Long
    Dim vWRDs As Variant, vBLRBs As Variant, vMSKs As Variant
    Dim rw As Long, r As Long, rndm As Long, str As String

    'appTGGL bTGGL:=False  'uncomment after testing

    getReplacements vWRDs
    'Debug.Print LBound(vWRDs, 1) & ":" & UBound(vWRDs, 1)
    'Debug.Print LBound(vWRDs, 2) & ":" & UBound(vWRDs, 2)

    With Worksheets("Blurbs")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion

            'replace with associated GUID for unique-per-term
            With .Resize(.Rows.Count - 1, 1).Offset(1, 1)
                'reset column B to column A values
                .Cells = .Offset(0, -1).Value2
                For w = LBound(vWRDs, 1) To UBound(vWRDs, 1)
                    Debug.Print vWRDs(w, 1) & " - " & vWRDs(w, 4)
                    .Replace what:=vWRDs(w, 1), lookat:=xlPart, MatchCase:=True, _
                             replacement:=vWRDs(w, 4)
                Next w
            End With

            'replace GUIDs with associated ANCHOR elements
            With .Resize(.Rows.Count - 1, 1).Offset(1, 1)
                For w = LBound(vWRDs, 1) To UBound(vWRDs, 1)
                    .Replace what:=vWRDs(w, 4), lookat:=xlPart, MatchCase:=True, _
                             replacement:=Replace(Replace(csANCHOR, "×LL×", vWRDs(w, 2)), "×FN×", vWRDs(w, 1))
                Next w
            End With

        End With
    End With

    appTGGL
End Sub

Sub getReplacements(ByRef wrds As Variant)
    With Worksheets("Replacements")
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, 1).Offset(1, 2)
                .FormulaR1C1 = "=LEN(RC1)"
                .Value = .Value2
            End With
            With .Resize(.Rows.Count - 1, 1).Offset(1, 3)
                .Formula = "=getGuid()"
                .Value = .Value2
            End With
            .Cells.Sort key1:=.Columns(3), order1:=xlDescending, _
                        key2:=.Columns(1), order2:=xlAscending, _
                        Orientation:=xlTopToBottom, Header:=xlYes
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                wrds = .Cells.Value2
            End With
        End With
    End With
End Sub

Function getGuid() As String
    Dim tl As Object
    Set tl = CreateObject("Scriptlet.TypeLib")
    getGuid = tl.Guid
    Set tl = Nothing
End Function

Sub appTGGL(Optional bTGGL As Boolean = True)
    With Application
        .EnableEvents = bTGGL
        .ScreenUpdating = bTGGL
        .DisplayAlerts = bTGGL
        .Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual)
    End With
End Sub

通过重复点击[F8]键来逐步完成此操作,以了解逻辑和流程。

替换工作表

replacement_values

Blurbs工作表

blurbs_worksheet

<强>结果

Lorem <a href="http://www.apple.com/uk/osx">Mac OS X</a> dolor sit amet, consectetur <a href="http://www.ibm.com/">IBM</a> elit.
<br/>
Quisque <a href="http://www.ibm.com/">IBM</a> dolor <a href="http://www.apple.com/uk/mac">Mac</a> ante vestibulum, eget <a href="https://www.microsoft.com/">Microsoft</a> sapien tempus.
<br/>
Duis tristique sapien non <a href="https://www.microsoft.com/">Microsoft</a> <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a> porta.
<br/>
Praesent <a href="http://www.apple.com/uk/ios">Mac iOS</a> urna id metus fringilla, non mattis sem porttitor.
<br/>
Nunc bibendum <a href="http://www.ibm.com/">IBM</a> <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a> ligula varius vestibulum.
<br/>
Nulla sollicitudin elit nec mauris <a href="http://www.ibm.com/">IBM</a> <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a>
<br/>
Duis <a href="http://www.ibm.com/">IBM</a> <a href="http://www.apple.com/uk/osx">Mac OS X</a> id <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a> volutpat.
<br/>
Nullam <a href="http://www.apple.com/uk/ios">Mac iOS</a> dolor sed <a href="https://www.microsoft.com/">Microsoft</a> consequat <a href="https://www.microsoft.com/en-gb/windows">Windows</a> quis eu purus.
<br/>
Nullam <a href="https://www.microsoft.com/">Microsoft</a> dolor eget <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a> <a href="http://www.apple.com/uk/osx">Mac OS X</a> <a href="http://www.apple.com/uk/ios">Mac iOS</a>
<br/>
Vivamus <a href="http://www.apple.com/uk/mac">Mac</a> leo non <a href="https://www.microsoft.com/en-gb/windows">Windows</a> pharetra pretium a malesuada dolor.
<br/>
Donec condimentum leo <a href="https://www.microsoft.com/en-gb/windows">Windows</a> dictum <a href="http://www.ibm.com/">IBM</a>
<br/>
Nullam aliquam velit <a href="https://www.microsoft.com/en-gb/windows">Windows</a> ullamcorper <a href="https://www.microsoft.com/">Microsoft</a>
<br/>
Curabitur <a href="https://www.microsoft.com/en-gb/windows">Windows</a> leo eget magna eleifend, <a href="http://www.apple.com/uk/osx">Mac OS X</a> posuere velit tincidunt.
<br/>
Aenean pulvinar quam <a href="http://www.apple.com/uk/mac">Mac</a> <a href="http://www.apple.com/uk/ios">Mac iOS</a> <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a>
<br/>
Aliquam <a href="https://www.microsoft.com/">Microsoft</a> diam non ipsum egestas <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a>
<br/>
Phasellus in lorem <a href="http://www.ibm.com/">IBM</a> <a href="http://www.apple.com/uk/ios">Mac iOS</a> mauris tempus, laoreet nunc.
<br/>
Vestibulum accumsan justo eu <a href="http://www.ibm.com/">IBM</a> tristique, <a href="https://www.microsoft.com/en-gb/windows">Microsoft Windows</a> massa ornare.
<br/>
Proin lobortis quam <a href="https://www.microsoft.com/en-gb/windows">Windows</a> sem imperdiet, nec <a href="http://www.apple.com/uk/ios">Mac iOS</a> enim aliquet.