Edit Txt file data

Chewyhairball

Active Member
Joined
Nov 30, 2017
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi

I have thousands of lines of data comprised of the 2 types below and I would like to edit the numbers so they are to 1 decimal place.

<Test:Module>2</Test:Module> would become <Test:Module>2.0</Test:Module>
<Testb:Moduleb>4.2222</Testb:Moduleb> would become <Testb:Moduleb>4.2</Testb:Moduleb>

I currently import the data from a txt file into column A Sheet 1.
Sheet 2 has formulas in column A to do this task which I then copy and paste into a new txt file.

So my questions are
  1. can I do this using VBA before copying and pasting back into a txt file and if so how.
  2. Can I use vba to alter the txt file directly and if so how.

Thanks for any help with this.
Thank Rory
 
I was playing with this, I had assumed you wanted to edit the text file without ever importing it into cells in Excel.
The below checks that the word "test" and "module" exist on the line, it then edits that line so the number is formatted as "0.0".
It does so in an array and the spits the array back out into the text file. Nothing is ever imported to Excel.

VBA Code:
Sub EditTxtFile()
    Dim fPath As String, fCont As Variant, fNum As Integer
    Dim x As Long, str As String, var As Variant

    fPath = "C:\Users\jbloggs\Desktop\test.txt" ' text file location, change to suit
    fNum = FreeFile
    Open fPath For Input As #fNum
    fCont = Split(Input(LOF(fNum), fNum), vbNewLine)
    For x = 0 To UBound(fCont)
        str = fCont(x)
        If InStr(str, "Test") > 0 And InStr(str, "Module") > 0 Then
            var = Split(Replace(str, ">", "<"), "<")
            fCont(x) = "<" & var(1) & ">" & Format(var(2), "0.0") & "<" & var(3) & ">"
        End If
    Next x
    Close #fNum
   
    Open fPath For Output As #1
    Print #fNum, Join(fCont, vbNewLine)
    Close #fNum
End Sub
This works great! :) I am going to be cheeky now and ask if something else is possible. I maybe should have started with this but I wasnt sure if it was possible.

The text file that I am altering actually comes from an xml file produced from a database. For various reasons both the database and xml files cannot be altered and the only thing I can do is manually open the xml as txt file edit that then save as an xml. Both of your suggestions have saved me lots of headaches but I am wondering now if the opening the xml file as txt can be done.

1.Open xml file as txt
2.Convert data to 1 decimal place(which you have done)
3.Save back to xml file

appologies if I should be starting another thread for this.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
The xml file should work in the same way the txt file does, try is as below:
VBA Code:
Sub EditTxtFile()
    Dim fPath As String, fCont As Variant, fNum As Integer
    Dim x As Long, str As String, var As Variant

    fPath = "C:\Users\jbloggs\Desktop\test.xml" ' xml file location, change to suit
    fNum = FreeFile
    Open fPath For Input As #fNum
    fCont = Split(Input(LOF(fNum), fNum), vbNewLine)
    For x = 0 To UBound(fCont)
        str = fCont(x)
        If InStr(str, "Module") > 0 Then
            var = Split(Replace(str, ">", "<"), "<")
            fCont(x) = "<" & var(1) & ">" & Format(var(2), "0.0") & "<" & var(3) & ">"
        End If
    Next x
    Close #fNum
    
    Open fPath For Output As #1
    Print #fNum, Join(fCont, vbNewLine)
    Close #fNum
End Sub
 
Upvote 0
Assuming they are all to be 1 decimal place try this. For testing, assumes data in col A and results in col B.

VBA Code:
Sub Edit_To_1_DP()
  Dim a As Variant
  Dim i As Long, p1 As Long, p2 As Long
  Dim s As String
  
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    s = a(i, 1)
    p1 = InStr(s, ">")
    p2 = InStr(2, s, "<")
    a(i, 1) = Left(s, p1) & Format(Val(Mid(s, p1 + 1)), "0.0") & Mid(s, p2)
  Next i
  Range("B1").Resize(UBound(a)).Value = a
End Sub


If it is just the 'Module' lines then try
VBA Code:
Sub Edit_To_1_DP_v2()
  Dim a As Variant
  Dim i As Long, p1 As Long, p2 As Long
  Dim s As String
  
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    s = a(i, 1)
    If InStr(1, s, "Module", vbTextCompare) > 0 Then
      p1 = InStr(s, ">")
      p2 = InStr(2, s, "<")
      a(i, 1) = Left(s, p1) & Format(Val(Mid(s, p1 + 1)), "0.0") & Mid(s, p2)
    End If
  Next i
  Range("B1").Resize(UBound(a)).Value = a
End Sub


Just a minor alteration to include 'or'
VBA Code:
If InStr(1, s, "Module", vbTextCompare) > 0 Or InStr(1, s, "moduleb", vbTextCompare) > 0 Then
and to include a /
VBA Code:
 p2 = InStr(2, s, "</")

and it does exactly what I wanted. Brilliant. thank you
 
Upvote 0
The xml file should work in the same way the txt file does, try is as below:
VBA Code:
Sub EditTxtFile()
    Dim fPath As String, fCont As Variant, fNum As Integer
    Dim x As Long, str As String, var As Variant

    fPath = "C:\Users\jbloggs\Desktop\test.xml" ' xml file location, change to suit
    fNum = FreeFile
    Open fPath For Input As #fNum
    fCont = Split(Input(LOF(fNum), fNum), vbNewLine)
    For x = 0 To UBound(fCont)
        str = fCont(x)
        If InStr(str, "Module") > 0 Then
            var = Split(Replace(str, ">", "<"), "<")
            fCont(x) = "<" & var(1) & ">" & Format(var(2), "0.0") & "<" & var(3) & ">"
        End If
    Next x
    Close #fNum
    
    Open fPath For Output As #1
    Print #fNum, Join(fCont, vbNewLine)
    Close #fNum
End Sub

Hi

This worked perfect and has now given me a tool to literally save me doing anything.

thanks
 
Upvote 0
I want to say a massive thank you to both of you for giving me a working solution. I can see me using both of them at some point, not just for my question today.
I am always impressed when I come on here and this time is no exception.

Thank you guys

:)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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