VBAsearchstring和replace

假设我们有一个名为text.txt的文本文件。 在这个文本文件中有两条感兴趣的内容。

src="somethingrandom1111.png" src="morerandomnumberstuffthisnamewillvary.png" 

我想要做的是search整个文档的src =“,然后用其他stringreplace后面的所有内容,直到find另一个”。 所以,

 src="somethingrandom1111.png" src="morerandomnumberstuffthisnamewillvary.png" 

会成为

 src="thiswasreplace.gif" src="thistoo!.jpg" 

这是我的代码的一部分:

 TempFile = Environ$("temp") & "\" & "index.htm" With TempWB.PublishObjects.Add( _ SourceType:=xlSourceRange, _ Filename:=TempFile, _ Sheet:=TempWB.Sheets(1).Name, _ Source:=TempWB.Sheets(1).UsedRange.Address, _ HtmlType:=xlHtmlStatic) .Publish (True) End With Set fso = CreateObject("Scripting.FileSystemObject") Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, 0) ts = Replace(ts, 'src="', something) ts.Close 

一两个问题。
– 将行为replace为string,而不是像在日常工作中那样对文本stream对象进行replace。
– 此外,它看起来好像要search多个string,并为每个string具有不同的replace值。

以下代码显示了一个这样的方法。 我用早期的绑定和VBscript正则expression式引擎做replace。 我将“查找string/replacestring”存储在硬编码数组中,以便于访问。 我使用OpenTextFile方法而不是GetFile,但没有特别的原因。

你应该能够适应你的要求:

 Option Explicit Option Base 0 Sub FindReplaceInFile() Const sPath = "C:\users\ron\desktop\" Const sFN As String = "Text.txt" Dim FSO As FileSystemObject Dim TS As TextStream Dim S As String Dim arrFindReplace() As Variant Dim RE As RegExp Dim I As Long Dim sPat As String, sRepl As String arrFindReplace = Array(Array("somethingrandom1111.png", "thiswasreplace.gif"), _ Array("morerandomnumberstuffthisnamewillvary.png", "thistoo!.jpg")) Set FSO = New FileSystemObject Set TS = FSO.OpenTextFile(sPath & sFN, ForReading) S = TS.ReadAll TS.Close Set RE = New RegExp With RE .Global = True .IgnoreCase = True End With For I = 0 To UBound(arrFindReplace) sPat = "(src="")" & arrFindReplace(I)(0) & """" sRepl = "$1""" & arrFindReplace(I)(1) & """" With RE .Pattern = sPat S = .Replace(S, sRepl) End With Next I Set TS = FSO.OpenTextFile(sPath & sFN, ForWriting) TS.Write S TS.Close End Sub 

你需要为此使用正则expression式。 在Excel Visual Studio中激活正则expression式引擎。 按照步骤

  1. 启动Microsoft Visual Basic Studio。

  2. 在文件菜单上,单击新build项目。

  3. 单击新build项目对话框中的标准EXE,然后单击确定。

  4. 在项目菜单上,单击引用。

  5. 双击Microsoft VBScript正则expression式5.5,然后单击确定。

这是守则

 Sub RegEx_Macro() TempFile = Environ$("temp") & "\" & "index.htm" With TempWB.PublishObjects.Add( _ SourceType:=xlSourceRange, _ Filename:=TempFile, _ Sheet:=TempWB.Sheets(1).Name, _ Source:=TempWB.Sheets(1).UsedRange.Address, _ HtmlType:=xlHtmlStatic) .Publish (True) End With Set fso = CreateObject("Scripting.FileSystemObject") Set ts = fso.OpenTextFile(TempFile, 1) strdata = ts.ReadAll Dim objRegExp As RegExp Dim objMatch As Match Dim colMatches As MatchCollection Dim RetStr As String ' Create a regular expression object. Set objRegExp = New RegExp 'Set the pattern by using the Pattern property. objRegExp.Pattern = "src="".*?""" ' Set Case Insensitivity objRegExp.IgnoreCase = True 'Set global applicability. objRegExp.Global = True Set colMatches = objRegExp.Execute(strdata) ' Execute search. For Each objMatch In colMatches ' Iterate Matches collection. strtoreplace = InputBox("Enter string to be replaced" , "Enter Replacement String For"& VbCrLf & objMatch.Value ) 'Give string to be replaced in inputbox strdata = Replace(strdata, objMatch.Value, strtoreplace) Next 'ts.Close Set ts = fso.OpenTextFile(TempFile, 2) ts.Write strdata ts.Close End Sub 

希望这可以帮助 !!