在Excel中查找/replace表中的多个子string

我将复制/粘贴文本放入Excel中的一个单元格中,该单元格包含特定的需要replace的子string,从大约4000行的列表中。 我有相同数量的文本/内容作为行数,我需要手动复制/粘贴。

本质上,特定的子string需要用相同的子stringreplace,用HTML链接包装,但是这需要发生在我的列表中的任何子string的所有事件。

例:

我有这样的文字:

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

在表#2中,我有以下;

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

<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.

我不是专家,所以我一直没有find解决办法。 有一个关于SO的答案,提出了另外一个问题: =SUBSTITUTE(A2,"Author","Authoring")但我不知道如何编辑这个包括所有的string,而不需要手动添加每一个公式,因为这目前只是取代每个string逐个。

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

在这种情况下,你面临的最大问题是误报。 当一个单词或短语可以在另一个单词或短语中find时,可能会导致“双重处理”或错误地处理search词。 为避免这种情况,请按照长度从大到小的顺序处理每个术语,并分两步进行。 首先,用一个绝对唯一的临时stringreplacesearch词,一旦所有的词已被重新分配,返回并用实际的HTML锚元素replace临时string。

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]键来了解逻辑和stream程。

replace工作表

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.