+ Reply to Thread
Results 1 to 3 of 3

Text document edition and manipulation

Hybrid View

hinubhai Text document edition and... 01-18-2015, 05:42 PM
karedog Re: Text document edition and... 01-18-2015, 11:15 PM
jindon Re: Text document edition and... 01-19-2015, 12:50 AM
  1. #1
    Registered User
    Join Date
    10-17-2012
    Location
    Essen, Germany
    MS-Off Ver
    Excel 2010
    Posts
    2

    Text document edition and manipulation

    Hi everyone,
    I am trying to write a Macro than can modify a text documents and edit it according to my requirement. For example i have a text document which contains lines:

    facet normal -6.930870e-001 -7.208539e-001 -0.000000e+000
    outer loop
    vertex 3.535534e+000 3.535534e+000 0.000000e+000
    vertex 3.394004e+000 3.671613e+000 5.000000e+000
    vertex 3.394004e+000 3.671613e+000 0.000000e+000
    end loop

    I would like to edit this text file select only the numerical values in the loop and make them comma separated. This text would look like
    3.535534e+000, 3.535534e+000, 0.000000e+000
    3.394004e+000, 3.671613e+000, 5.000000e+000
    3.394004e+000, 3.671613e+000, 0.000000e+000

    Also what would you do if you have n number of loops? I have read that the following manipulation has to be done using Strings. I shall be grateful if you could provide a Macro to this.

    Thanks.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Text document edition and manipulation

    Hi,

    This code will change the data to desired format. It will also ask for the range where the macro should be performed, so you don't need to bother about loops.

    Sub Test()
      Dim rng As Range, cell As Range, str1 As String, v, i As Long
    
      On Error Resume Next
        Set rng = Application.InputBox(prompt:="Select the range", Type:=8)
        If Err.Number <> 0 Then
           Err.Clear
           Exit Sub
        End If
      On Error GoTo 0
    
      Application.ScreenUpdating = False
      For Each cell In rng
          str1 = ""
          v = Split(cell.Value, Space(1))
          For i = LBound(v) To UBound(v)
              If IsNumeric(v(i)) Then str1 = str1 & ", " & v(i)
          Next i
          cell.Value = Mid(str1, 3)
      Next cell
      Application.ScreenUpdating = True
    End Sub
    Regards
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Text document edition and manipulation

    Try this
    Sub test()
        Dim fn As String, txt As String, e, x, temp As String
        fn = Application.GetOpenFilename("TextFile,*.txt")
        If fn = "False" Then Exit Sub
        txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
        x = Split(txt, "outer loop" & vbCrLf): txt = ""
        With CreateObject("VBScript.RegExp")
            .Global = True: .IgnoreCase = True
            For Each e In x
                If e Like "*end loop*" Then
                    temp = Split(e, vbCrLf & "end loop")(0)
                    .Pattern = "(\d)(?= ) "
                    temp = .Replace(temp, "$1,")
                    .Pattern = "[a-z]+ "
                    temp = .Replace(temp, "")
                    txt = txt & vbCrLf & temp
                End If
            Next
        End With
        Open Replace(fn, ".txt", "_revised.txt") For Output As #1
            Print #1, Mid$(txt, 3)
        Close #1
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 09-04-2014, 03:32 AM
  2. Text Manipulation Help
    By bbroussard in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-03-2014, 02:04 PM
  3. Text Manipulation
    By jcy1011 in forum Excel General
    Replies: 4
    Last Post: 08-02-2010, 03:41 PM
  4. Text Manipulation
    By sanjimmy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2007, 05:18 PM
  5. [SOLVED] For better Performance in VBA for Excel - Strings manipulation OR Objects manipulation
    By vmegha in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2005, 08:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1