VBA excel. Open textfile, search word with the first 3 character specified, then copy to textbox edit then owerwrite the original in the file. RRS feed

  • Question

  • Hi!

    I need solve a problem vith macro. I need a script, what open a text file, search word in the file waht begining specified 3 character, then write out the diferent of this word and make math and write in the filw owerwrite the original word.

    1 TextBox -> search directori and file name. (c:\DATAS\data.dat  search te word what begin "la=")

    2 TextBox -> the found character (found "la=510" or "la=522,25" -> what i want write to the textbox: 510 or 522,25)

    3 TextBox -> the diferent math (example: +100 or -10)

    then the original paramterer ex.: la=510 replace the new math parameter (510+100=610) la=610.

    Thank you!

    Tuesday, December 18, 2018 9:11 AM

All replies

  • I don't understand your description but I would use regular expressions to extract the data.

    Sub ExtractNumber()
      Dim regEx As Object
      Dim s As String
      Dim pat As String
      Dim matches As Object
      Dim match As Object
      Dim subMatch As Variant
      pat = "la=([0-9]+(\,[0-9]+)?)"
      s = "la=5100,000 la=500"
      Set regEx = CreateObject("vbscript.regexp")
      regEx.Global = True
      regEx.IgnoreCase = True
      regEx.Pattern = pat
      regEx.MultiLine = True
      Set matches = regEx.Execute(s)
      For Each match In matches
        If match.subMatches.Count > 0 Then
          Debug.Print match.subMatches(0)
        End If
      Next match
    End Sub

    Wednesday, December 19, 2018 2:08 AM
  • Sorry for my bad English! So I want to solve the problem of having to open a text file that should look for a college chain, which is the first 3 letters of a word. From this result, I need to have the rest of the word behind a = sign. this will be a variable length sequence.
    With this number, I want to perform a math operation and write the result back to the file overwriting the word you are looking for.

    For example .:

    in the file example tis is waht i search ("la=510") or ("la=516,25") or ("la=1")

    if find then summation or subtraction other valu from textbox... example (original parameter in file "la=515" the operatin and the valu from another textbox "+100" and owerwrite (replace) in the original file original string to "la=615")

    TextBox1.text = file location (c:\testfolder\)

    TextBox2.text = search caharacter ("la=")

    TextBox3.text = mathematical operation and value (+100)

    Wednesday, December 19, 2018 7:08 AM