I have an excel file that runs a model from an external .exe file, and collects its outputs to store on various sheets for data analysis.
The .exe file keeps its input files in an accessible text based format. I realised that I can create a batch run by editing the input files directly from vba and running in a loop, however it requires find and replace in a .txt or .xml file from VBA. I would need to use a wildcard as part of the string I want to find will change everytime and is unpredictable at the first iteration. E.g:
I want to find the following text within the text based file:
"0.3843 Flow_Rate_Mass kg/s"
and replace the number before rerunning my script to solve the .exe with this new input file for a new flow rate. However, the number 0.3843 could be anything in the first instance (in the format "x.xxxx Flow_Rate_Mass kg/s"
), so I need to use a wildcard in order to find and replace.
After the first iteration I will have set this value at the previous iteration, and so I can use find and replace from a known string without needing a wildcard.
So the following should work, but is not flexible enough at the first iteration:
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
sFileName = "C:\filelocation"
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, "0.3843 Flow_Rate_Mass kg/s", "0.5000 Flow_Rate_Mass kg/s")
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
Is there any work around without REGEX?