Solved

manipulating text

Posted on 2006-11-06
11
178 Views
Last Modified: 2010-04-30
109290511122555        *****WORKORDR1              NO
2Amount Charged  $6.00  

109290511122555        *****WORKORDR2              NO
2Amount Charged  $12.00  


109290511122555        *****WORKORDR3              NO
2Amount Charged  $14.00  


I have a text file with records similiar to the records above. I need to loop through the records and move the
dollar amount from line 2 and place it next to WORKORDR% in line 1. Does anyone know how I can accomplish this?
0
Comment
Question by:earngreen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 3

Expert Comment

by:fullcontact
ID: 17881612
I would create a new file.

Read the source file 2 lines at a time (checking for blank lines and ignoring them)
Rebuild the new string as required, then output to the new file.
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 17882479
Try this solution using Split and Join:

Option Explicit

Private Sub Command1_Click()
   Dim FF         As Integer
   Dim Length     As Long
   Dim sText      As String
   Dim vSplit()   As String
   Dim NewArray() As String
   Dim NewJoin    As String
   Dim lNewCount  As Long
   Dim pos        As Long
   Dim i          As Long
   
   FF = FreeFile
   Open App.Path & "/WorkOrder.txt" For Input As #FF
   Length = LOF(FF)
   sText = Input$(Length, FF)
   Close FF
   vSplit = Split(sText, vbCrLf)
   Debug.Print "UBound(vSplit)", UBound(vSplit)
   For i = 0 To UBound(vSplit)
      pos = InStr(vSplit(i), "$")
      If pos Then
         vSplit(i - 1) = vSplit(i - 1) & " " & Mid$(vSplit(i), pos)
         If lNewCount Then
            ReDim Preserve NewArray(lNewCount)
         Else
            ReDim NewArray(0)
         End If
         NewArray(lNewCount) = vSplit(i - 1)
         lNewCount = lNewCount + 1
         Debug.Print vSplit(i - 1)
      End If
   Next
   NewJoin = Join(NewArray, vbCrLf)
   FF = FreeFile
   Open App.Path & "/WorkOrderNew.txt" For Output As #FF
   Print #FF, NewJoin
   Close FF
End Sub

 
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17887289
@ earngreen

You need the new file content like this ?
========================

109290511122555        *****WORKORDR1 $6.00
109290511122555        *****WORKORDR2 $12.00  
109290511122555        *****WORKORDR3 $14.00
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:earngreen
ID: 17891236
I need the new file to look like this

109290511122555        *****WORKORDR16.00             NO
2Amount Charged  $6.00  

109290511122555        *****WORKORDR212.00           NO
2Amount Charged  $12.00


109290511122555        *****WORKORDR314.00           NO
2Amount Charged  $14.00
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 17891510
Option Explicit

Private Sub Command1_Click()
   Dim FF         As Integer
   Dim Length     As Long
   Dim sText      As String
   Dim vSplit()   As String
   Dim sValue     As String
   Dim pos        As Long
   Dim i          As Long
   Dim j          As Long
   
   FF = FreeFile
   Open App.Path & "/WorkOrder.txt" For Input As #FF
   Length = LOF(FF)
   sText = Input$(Length, FF)
   Close FF
   vSplit = Split(sText, vbCrLf)
   For i = 0 To UBound(vSplit)
      pos = InStr(vSplit(i), "$")
      If pos Then
         sValue = FormatNumber$(Val(Mid$(vSplit(i), pos + 1)), 2)
         pos = InStr(vSplit(i - 1), "WORKORDR") + 7
         If pos Then
            For j = pos To Len(vSplit(i - 1))
               If Mid$(vSplit(i - 1), j, 1) = " " Then
                  vSplit(i - 1) = Left$(vSplit(i - 1), j) & _
                     sValue & Mid$(vSplit(i - 1), j + 1)
                  Exit For
               End If
            Next
         End If
      End If
   Next
   For i = 0 To UBound(vSplit)
      Debug.Print vSplit(i)
   Next
   sText = Join(vSplit, vbCrLf)
   FF = FreeFile
   Open App.Path & "/WorkOrderNew.txt" For Output As #FF
   Print #FF, sText
   Close FF
End Sub



0
 

Author Comment

by:earngreen
ID: 17892172
danaseaman
 Your code is similiar to what i need but the dollar amount may vary and I am not sure if your code would do the job. i have the following code that i have used in the past to get the position of dollor amount. it will always lie in position 18. Would there be a way to call this value in your code and append it to the WORKORDR3 field.

109290511122555        *****WORKORDR3              NO
2Amount Charged  $14.00  


Public Sub GetValue()
            If Left(OutRec, 1) = "2" Then
            X = Mid(OutRec, 18)
            X = Left(X, InStr(1, X, " ") - 0)
            MinValue = Val(X)
        End If
End Sub

0
 

Author Comment

by:earngreen
ID: 17902156
danaseaman

With your code instead of looking for the $ is there a way to look for that position on line two?

Thanks
0
 
LVL 22

Accepted Solution

by:
danaseaman earned 500 total points
ID: 17902380
Option Explicit

Private Sub Command1_Click()
   Dim FF         As Integer
   Dim Length     As Long
   Dim sText      As String
   Dim vSplit()   As String
   Dim sValue     As String
   Dim pos        As Long
   Dim i          As Long
   Dim j          As Long
   
   FF = FreeFile
   Open App.Path & "/WorkOrder.txt" For Input As #FF
   Length = LOF(FF)
   sText = Input$(Length, FF)
   Close FF
   vSplit = Split(sText, vbCrLf)
   For i = 0 To UBound(vSplit)
      If Left$(vSplit(i), 1) = "2" Then
         sValue = FormatNumber$(Val(Mid$(vSplit(i), 19)), 2)
         pos = InStr(vSplit(i - 1), "WORKORDR") + 7
         If pos Then
            For j = pos To Len(vSplit(i - 1))
               If Mid$(vSplit(i - 1), j, 1) = " " Then
                  vSplit(i - 1) = Left$(vSplit(i - 1), j) & _
                     sValue & Mid$(vSplit(i - 1), j + 1)
                  Exit For
               End If
            Next
         End If
      End If
   Next
   For i = 0 To UBound(vSplit)
      Debug.Print vSplit(i)
   Next
   sText = Join(vSplit, vbCrLf)
   FF = FreeFile
   Open App.Path & "/WorkOrderNew.txt" For Output As #FF
   Print #FF, sText
   Close FF
End Sub


0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17904286
@ earngreen , try this

Private Sub Command1_Click()
Dim sFileContent As String
Dim sNewFileContent As String
    sFileContent = "109290511122555        *****WORKORDR1              NO" & _
        vbCrLf & "2Amount Charged  $6.00" & _
        vbCrLf & "109290511122555        *****WORKORDR2              NO" & _
        vbCrLf & "2Amount Charged  $12.00" & _
        vbCrLf & "109290511122555        *****WORKORDR3              NO" & _
        vbCrLf & "2Amount Charged  $14.00"
    sNewFileContent = ExtractContent(sFileContent)
    MsgBox sNewFileContent '<---- Save this to new file
End Sub
Private Function ExtractContent(FileContent As String) As String
Dim oReg As New RegExp
Dim oMat, oMCol
Dim sResult As String
    Set oReg = CreateObject("VBScript.RegExp")
    oReg.IgnoreCase = True
    oReg.Global = True
    sResult = vbCrLf & FileContent & vbCrLf
    oReg.Pattern = "(\n\d+[ \t]+\*{5}WORKORDR\d+)([ \t]+NO\r\n\d+Amount Charged[ \t]+\$)(\d+\.\d+)([ \t]*\r)"
    Set oMCol = oReg.Execute(vbCrLf & FileContent & vbCrLf)
    For Each oMat In oMCol
        sResult = Replace(sResult, oMat.Value, oReg.Replace(oMat.Value, "$1$3$2$3$4"))
    Next
    sResult = Left(sResult, Len(sResult) - 2)  'Removing vbCrLf
    sResult = Right(sResult, Len(sResult) - 2) 'Removing vbCrLf
    ExtractContent = sResult
    Set oReg = Nothing
End Function
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17904458
Change this line
>>Dim oReg As New RegExp
to
Dim oReg
0
 

Author Comment

by:earngreen
ID: 17906672
danaseaman

That worked great!

Thanks a lot for the help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question