LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replacing a string within a text file


Hi,

I an trying to put together code that will let me replace text in
text file. I have copied a previous example off this web site and i
works well. The only trouble is I want to find a string within a tex
file and replace not only that string but the following 10 strings.

The example below replaces whats in cell A1 with B1 but I would lik
this to be extended to L2, is this possible?

Many Thanks
Dave

Sub ReplaceTextInFile(SourceFile As String, _
sText As String, rText As String)


Dim TargetFile As String, tLine As String, tString As String
Dim p As Integer, i As Long, F1 As Integer, F2 As Integer
SourceFile = "C:\Documents and Settings\Chri
Hallam\Desktop\origionalRESULT.txt"
TargetFile = "C:\Documents and Settings\Chris Hallam\Desktop\New.txt"

If Dir(SourceFile) = "" Then Exit Sub
If Dir(TargetFile) < "" Then
On Error Resume Next
Kill TargetFile
On Error GoTo 0
If Dir(TargetFile) < "" Then
'MsgBox TargetFile & _
'" already open, close and delete / rename the file an
try again.", _
vbCritical
Exit Sub
End If
End If


F1 = FreeFile
Open SourceFile For Input As F1
F2 = FreeFile
Open TargetFile For Output As F2
i = 1 ' line counter
Application.StatusBar = "Reading data from " & _
TargetFile & " ..."

While Not EOF(F1)
If i Mod 100 = 0 Then Application.StatusBar = _
"Reading line #" & i & " in " & _
TargetFile & " ..."
Line Input #F1, tLine
If sText < "" Then
ReplaceTextInString tLine, sText, rText
End If
Print #F2, tLine
i = i + 1
Wend

Application.StatusBar = "Closing files ..."

Close F1
Close F2

Kill SourceFile ' delete original file
Name TargetFile As SourceFile ' rename temporary file

Application.StatusBar = False

End Sub

Private Sub ReplaceTextInString(SourceString As String, _
SearchString As String, ReplaceString As String)
Dim p As Integer, NewString As String
Do
p = InStr(p + 1, UCase(SourceString), UCase(SearchString))
If p 0 Then ' replace SearchString with ReplaceString
NewString = ""
If p 1 Then NewString = Mid(SourceString, 1, p - 1)
NewString = NewString + ReplaceString
NewString = NewString + Mid(SourceString, _
p + Len(SearchString), Len(SourceString))
p = p + Len(ReplaceString) - 1
SourceString = NewString
End If
If p = Len(NewString) Then p = 0
Loop Until p = 0
End Sub

Sub TestReplaceTextInFile()
ReplaceTextInFile ThisWorkbook.Path & _
"\ReplaceInTextFile.txt", Range("a2"), Range("b1")
End Su

--
DHalla
-----------------------------------------------------------------------
DHallam's Profile: http://www.excelforum.com/member.php...fo&userid=2525
View this thread: http://www.excelforum.com/showthread.php?threadid=38830

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting and replacing the first word in a String of text RAYCV Excel Worksheet Functions 5 August 21st 09 01:15 PM
Replacing numeric X-axis labels with corresponding text string Cinco Excel Discussion (Misc queries) 2 July 25th 09 11:13 PM
Replacing a random string of 5 numbers Katie59 Excel Discussion (Misc queries) 5 March 23rd 06 09:12 PM
Formula for Replacing Text in a String? David Godinger Excel Programming 7 May 16th 05 02:05 PM
Replacing a value within a string using a formula solo_razor[_17_] Excel Programming 2 October 30th 03 09:46 AM


All times are GMT +1. The time now is 12:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"