Repleace in a text file

Philip00

New Member
Joined
Aug 16, 2017
Messages
37
Hi All,

I have a following code in VBa that I am using

VBA Code:
Sub replace_txt()
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String

'Lewis Hamilton
'File to edit
sFileName = "F:\Program Files (x86)\Steam\steamapps\common\Automobilista\GameData\Talent\F1-2019\LHamilton.rcd"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
'Replace text
sTemp = Replace(sTemp, "Speed=" & Range("j2"), "Speed=" & Range("k2"))
'Create new file and write out sTemp which is the original file with the replaced text
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile("F:\Program Files (x86)\Steam\steamapps\common\Automobilista\GameData\Talent\F1-2019\LHamilton.rcd")
oFile.WriteLine sTemp
oFile.Close
End Sub

Instead of using "Range("j2")" value from my excel which can be different in a text file ( and in this case the cript will not do anythnig). Is it possible to use something like "Speed=" and two character to the right?

Is there any solution for this?

Thanks in advance,

Philip
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try:
VBA Code:
sTemp = Replace(sTemp, "Speed=??", "Speed=" & Range("k2"))
 
Upvote 0
How about replacing:

VBA Code:
sTemp = Replace(sTemp, "Speed=" & Range("j2"), "Speed=" & Range("k2"))

with:

VBA Code:
SearchString = "Speed="
sTemp = Left(sTemp, InStr(sTemp, SearchString) - 1 + Len(SearchString)) & Range("k2").Value & Mid(sTemp, InStr(sTemp, SearchString) - 1 + Len(SearchString) + Len(Range("k2").Value) + 1)

That assumes that you will be replacing equal number of characters. Example 2 for 2, 3 for 3, etc.
 
Upvote 0
How about replacing:

VBA Code:
sTemp = Replace(sTemp, "Speed=" & Range("j2"), "Speed=" & Range("k2"))

with:

VBA Code:
SearchString = "Speed="
sTemp = Left(sTemp, InStr(sTemp, SearchString) - 1 + Len(SearchString)) & Range("k2").Value & Mid(sTemp, InStr(sTemp, SearchString) - 1 + Len(SearchString) + Len(Range("k2").Value) + 1)

That assumes that you will be replacing equal number of characters. Example 2 for 2, 3 for 3, etc.
That worked like a charm, thank you!
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,129
Members
449,097
Latest member
mlckr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top