VBA新手,但试图为以下内容制作一个宏:
谢谢你的帮助!
电子表格示例: spreadsheet
请注意,我希望能够找到并替换不必与原始文本具有相同字符数的文字。
到目前为止的尝试如下。运行调试时,strText
在悬停时是正确的,然后当它通过Get #i, , strText
步骤时,它将变为我正在尝试更新的文本文件的标题。
Sub Replace_Text()
Dim src As String, fl As String
Dim strFile As String
Dim i As Integer
Dim x As Integer
Dim strText As String
Dim lngMyRow As Long
Dim lngLastRow As Long
i = FreeFile
Application.ScreenUpdating = False
lngLastRow = Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
For lngLastRow = 2 To lngLastRow
x = 2
'Source directory
src = Range("A" & x)
'File name
strFile = Range("B" & x)
With CreateObject("vbscript.regexp")
.Global = True
Open src & "\" & strFile For Binary Access Read Write As #i
strText = Space(LOF(i))
Get #i, , strText
For Each cell In Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
.Pattern = Replace(Replace(Replace(Replace(cell.Value, "?", "\?"), "*", "\*"), "+", "\+"), ".", "\.")
strText = .Replace(strText, cell.Offset(, 1).Value)
Next cell
Put #i, , strText
Close #i
End With
i = i + 1
Next lngLastRow
MsgBox "done"
Application.ScreenUpdating = True
End Sub