我有一个配置文件如下:
[config1]
V1 = 1
V2 = 2
V2 = 3
[config2]
V1 = 4
V2 = 5
V3 = 6
如果V1为1,我想在每个配置上更改V3。
有没有办法使用excel VBA执行此操作?该文件保存为txt文件。
答案 0 :(得分:0)
这是Steve Rindsberg的搜索和替换程序,可以帮助您入门。您必须根据您的条件插入 If THEN 语句(或类似内容)进行替换。我对编辑文本文件并不熟悉,但认为这可能会有所帮助。
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