Vba Excel宏来查找和替换任何文本文件中的多个字符串
我对脚本很幼稚,但我使用了您站点中的以下代码来替换文本文件中的字符串,效果很好。但是,我不想指定文件名,它应该准备好像(*.txt或*.xml)这样的任何文件。因此,如果有人能在这方面提供帮助,我将不胜感激 谢谢Vba Excel宏来查找和替换任何文本文件中的多个字符串,vba,excel,Vba,Excel,我对脚本很幼稚,但我使用了您站点中的以下代码来替换文本文件中的字符串,效果很好。但是,我不想指定文件名,它应该准备好像(*.txt或*.xml)这样的任何文件。因此,如果有人能在这方面提供帮助,我将不胜感激 谢谢 Sub ReplaceStringInFile() Dim sBuf As String Dim sTemp As String Dim iFileNum As Integer Dim sFileName As String ' Edit as needed sFileName =
Sub ReplaceStringInFile()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
' Edit as needed
sFileName = "C:\Temp\test.txt"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, "THIS", "THAT")
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
您可以迭代数组中的项
Sub FindAndReplaceText()
Dim FileName As String
Dim FolderPath As String
Dim FSO As Object
Dim I As Integer
Dim SearchForWords As Variant
Dim SubstituteWords As Variant
Dim Text As String
Dim TextFile As Object
'Change these arrays to word you want to find and replace
SearchForWords = Array("string1", "string2", "string3")
SubstituteWords = Array("string100", "string200", "string300")
'Change the folder path to where your text files are.
FolderPath = "C:\your_path_here\"
Set FSO = CreateObject("Scripting.FileSystemObject")
FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath & "\", FolderPath)
FileName = Dir(FolderPath & "\*.txt")
Do While FileName <> ""
FileSpec = FolderPath & FileName
'Read all the file's text into a string variable.
Set TextFile = FSO.OpenTextFile(FileSpec, 1, False)
Text = TextFile.ReadAll
TextFile.Close
'Scan the string for words to replace and write the string back to the file.
Set TextFile = FSO.OpenTextFile(FileSpec, 2, False)
For I = 0 To UBound(SearchForWords)
Debug.Print Text
Replace Text, SearchForWords(I), SubstituteWords(I)
Debug.Print Text
Next I
TextFile.Write Text
TextFile.Close
FileName = Dir()
Loop
End Sub
Sub FindAndReplaceText()
将文件名设置为字符串
将FolderPath设置为字符串
作为对象的Dim FSO
作为整数的Dim I
作为变体的单词
Dim替代词作为变体
将文本变暗为字符串
将文本文件作为对象
'将这些数组更改为要查找并替换的word
SearchForWords=数组(“string1”、“string2”、“string3”)
SubstituteWords=数组(“string100”、“string200”、“string300”)
'将文件夹路径更改为文本文件所在的位置。
FolderPath=“C:\your\u path\u here\”
设置FSO=CreateObject(“Scripting.FileSystemObject”)
FolderPath=IIf(右(FolderPath,1)“\”,FolderPath&“\”,FolderPath)
FileName=Dir(FolderPath&“\*.txt”)
文件名“”时执行此操作
FileSpec=FolderPath&FileName
'将文件的所有文本读入字符串变量。
设置TextFile=FSO.OpenTextFile(FileSpec,1,False)
Text=TextFile.ReadAll
文本文件,关闭
'扫描字符串以查找要替换的单词,然后将字符串写回文件。
设置TextFile=FSO.OpenTextFile(FileSpec,2,False)
对于I=0到UBound(SearchForWords)
调试。打印文本
替换文本、搜索单词(I)、替换单词(I)
调试。打印文本
接下来我
文本文件。写文本
文本文件,关闭
FileName=Dir()
环
端接头
非常好,它可以无缝工作。非常感谢您的即时回复。
Sub FindAndReplaceText()
Dim FileName As String
Dim FolderPath As String
Dim FSO As Object
Dim I As Integer
Dim SearchForWords As Variant
Dim SubstituteWords As Variant
Dim Text As String
Dim TextFile As Object
'Change these arrays to word you want to find and replace
SearchForWords = Array("string1", "string2", "string3")
SubstituteWords = Array("string100", "string200", "string300")
'Change the folder path to where your text files are.
FolderPath = "C:\your_path_here\"
Set FSO = CreateObject("Scripting.FileSystemObject")
FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath & "\", FolderPath)
FileName = Dir(FolderPath & "\*.txt")
Do While FileName <> ""
FileSpec = FolderPath & FileName
'Read all the file's text into a string variable.
Set TextFile = FSO.OpenTextFile(FileSpec, 1, False)
Text = TextFile.ReadAll
TextFile.Close
'Scan the string for words to replace and write the string back to the file.
Set TextFile = FSO.OpenTextFile(FileSpec, 2, False)
For I = 0 To UBound(SearchForWords)
Debug.Print Text
Replace Text, SearchForWords(I), SubstituteWords(I)
Debug.Print Text
Next I
TextFile.Write Text
TextFile.Close
FileName = Dir()
Loop
End Sub