issue with replace function for a text file using VBA

xerxes1

New Member
Joined
Jul 14, 2013
Messages
4
I am trying to replace the the string "VALUE=" with "VALUE= (Excel cell data)" where it first appears after a certain position within a text file (this certain position being "SOURCE").

The issue I'm having is that when I specify the start position within the REPLACE function, the newly saved text file will have deleted all the text prior to start position. I would like to make the replacement and retain all the text preceding the start position.

Please see the code I have below (based on Excel VBA 2010):


Sub Main()
Dim mFleNm As String, Fle As Long, mInfo As String, mLongFle As Long
Dim mFileName As String
Dim row As Long
Dim start As Long
mFleNm = BrowseForFile("C:\Projects\Excel File")
Fle = FreeFile
mFileName = Right(mFleNm, Len(mFleNm) - InStrRev(mFleNm, "\"))
mFileName = Left(mFileName, Len(mFileName) - 4)
Open mFleNm For Input As #Fle
mLongFle = LOF(Fle)
mInfo = Input(mLongFle, #Fle)
Close (Fle)
For row = 7 To 10
start = InStr(mInfo, "SOURCE")
mInfo = Replace(mInfo, "VALUE=", "VALUE= " & ActiveSheet.Cells(row + 1, "C"), start, 1)
Fle = FreeFile
mFleNm = ActiveSheet.Cells(row + 1, "B") & ".txt"
Open mFleNm For Binary As #Fle
Put #Fle, , mInfo
Close #Fle
Next
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Replace(mInfo, "VALUE=", "VALUE= " & ActiveSheet.Cells(row + 1, "C"), start, 1)

start is also the name of the parameter ( see prompt text saying Start:=1)

try using MyStart as your variable name

MyStart = InStr(mInfo, "SOURCE")

Replace(mInfo, "VALUE=", "VALUE= " & ActiveSheet.Cells(row + 1, "C"), MyStart, 1)
 
Upvote 0
Thanks for the prompt response.

I changed the variable name to MyStart throughout, but still the generated text files delete everything before "SOURCE".
 
Upvote 0
This is from the bottom of the help page for Replace



Remarks
The return value of the Replace function is a string, with substitutions made, that begins at the position specified by start and and concludes at the end of the expression string. It is not a copy of the original string from start to finish.


So replace returns a string Starting at Start!!

Learn something New everyday
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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