Hello, I have a *.txt file and inside it contains lines with information (each block of information in a new line). This information has a similar pattern: 2014.05.26 11:45:00,67.14975845410586
The next lines have the same structure, the difference is the date value and the number's value.
In the above line I want to remove the comma and all the information in the left. About the number I want the integer part and the first four decimals.
Example: 67.1497
I have tried with Trim, Right, Mid... but I cannot code it right.
You should use InStr() function to get the comma position,
next use Mid$() to retrieve the string at right of comma.
Read help file for how to use this functions.
Dim arr() As String
Dim tmp As String
Open App.Path & "\File.txt" For Input As #1
Open App.Path & "\NewFile.txt" For Output As #2
While Not EOF(1)
Line Input #1, tmp
arr = Split(tmp, ",")
Print #2, Round(arr(1), 4)
DoEvents
Wend
Close #1
Close #2
MsgBox "Done!"
Use Instr to get the position of the comma (save it in a variable, e.g. commaPos)
Use Instr to get the position of the period and add 4 to it (save it in a variable, e.g. lastPos)
Subtract commaPos from lastPos to get the Length you want to pull.
Use Mid$ to pull the string starting at commaPos + 1, and the length you calculated.
If you're not sure there will always be four or more digits after the decimal point, you might want to check that lastPos is not greater than the length of the string, and if it is, set it to the length of the string before calculating the length you want to pull.
While split will work if that is a fixed position where the comma is then I would just use Mid$() to get either the 7 characters you want or everything after the comma and round it off to 4 places.
If the comma may occur at different positions then I would use Instr() and Mid$() as that would be faster than using Split()
oops i thought you wanted all of the data - any way the process it the same just amend the output
Probably off on a tangeant there. In post #1 it was implied this is existing data taken from a file, to be "cleaned." Not data being created by the program.
There could be other issues too. Might be lines with no comma. Might be spaces after the comma to be trimmed. Might be lots of things. All can be handled by tweaking the code:
Before
After
Code:
Option Explicit
Private SuppressEvents As Boolean
Private Sub cmdClean_Click()
Dim Text As String
Dim OutPos As Long
Dim InPos As Long
Dim CommaPos As Long
Dim EndLinePos As Long
Dim MoveLen As Long
Dim ChunkToMove As String
cmdClean.Enabled = False
Text = Text1.Text
OutPos = 1
InPos = 1
Do While InPos <= Len(Text)
CommaPos = InStr(InPos, Text, ",")
If CommaPos < 1 Then CommaPos = InPos - 1
EndLinePos = InStr(InPos, Text, vbNewLine)
If EndLinePos < 1 Then
EndLinePos = Len(Text) + 1
Else
EndLinePos = EndLinePos + 2
End If
If CommaPos < EndLinePos Then InPos = CommaPos + 1
MoveLen = EndLinePos - InPos
'Remove leading spaces. If not desired, remove
'use of LTrim$() here:
ChunkToMove = LTrim$(Mid$(Text, InPos, MoveLen))
Mid$(Text, OutPos) = ChunkToMove
InPos = InPos + MoveLen
OutPos = OutPos + Len(ChunkToMove)
Loop
SuppressEvents = True
Text1.Text = Left$(Text, OutPos - 1)
SuppressEvents = False
End Sub
Private Sub Text1_Change()
If SuppressEvents Then Exit Sub
cmdClean.Enabled = True
End Sub
Zip, zam, zowie. Much quicker than "split-de-splits" or awful RegEx techniques, though not important unless large masses of data need to be cleaned.
the process of modifying a thing that comes in is to output the modification
whether the change is made to a line in a file or a placeholder in memory the approach is the same
the op needs to know that the line in the file can be read in as a and b where b is after the comma
and that b can be modified using the format$ function...
Now tell me how do you change the contents of a file so that it reflects the needs of the op without "outputing"/modifying something
where are they putting/keeping the changed value/shape or B
here to wonder if you are reading what i am writting?
its about the OP learning some thing new
(we should not be fighting each other here)
here to be as helpful as possible
what follows is an example of what the op could do using the technique i have suggested
there is enough here to help the OP understand how data can be read and manipulated in vb6
Code:
Dim strEmpFileName As String
Dim strBackSlash As String
Dim intEmpFileNbr As Integer
Dim strEmpName As String
Dim intDeptNbr As Integer
Dim strJobTitle As String
Dim dtmHireDate As Date
Dim sngHrlyRate As Single
strBackSlash = IIf (Right$(App.Path, 1) = "\", "", "\")
strEmpFileName = App.Path & strBackSlash & "EMPLOYEE.DAT"
intEmpFileNbr = FreeFile
Open strEmpFileName For Input As #intEmpFileNbr
Do Until EOF(intEmpFileNbr)
Input #intEmpFileNbr, strEmpName, intDeptNbr, strJobTitle, dtmHireDate, sngHrlyRate
Print strEmpName; _
Tab(25); Format$(intDeptNbr, "@@@@"); _
Tab(35); strJobTitle; _
Tab(55); Format$(dtmHireDate, "mm/dd/yyyy"); _
Tab(70); Format$(Format$(sngHrlyRate, "Standard"), "@@@@@@@")
Loop
here to help
Last edited by incidentals; May 27th, 2016 at 04:42 PM.