DMS500Tech
Technical User
(I have serched the help files of VBA endlessly for a solution but it appears that I don't know how to ask my question)
I have the following code that opens a text file searches for certain strings and it works great as long as what I am looking for is in a fixed location but it needs to be expanded to search for a variable that is never in the same location within the string.
sample line 1:
LEN: OKCR 02 0 04 05
sample line 2:
DN 4095557200
What I can't figure out is how to create a search for a third set of characters that moves to different postitions from line to line.
sample line 3:
ACB NOAMA SCWID CND NOAMA CNAMD NOAMA CXR CTALL N STD ACRJ INACT
from sample 3 I need to find each line that contains "CXR" and copy it plus the next 15 characters to column 3 row X.
As I stated "CXR" may be in any position from 1 to 76.
Something I just thought about that is also a problem is that after the "CXR" the remaing data may wrap to the next line (if that is an easy fix then okay but I can live without it).
HERE IS WHAT I HAVE NOW:
Sub Extract_DN_and_LEN_from_a_text_files_WORKS()
'Open a text file and extract data and place it in an Excell spread sheet
'one line at a time
Dim intOutFile As String
intOutFile = FreeFile
Dim RecordNum As Long
Dim LinesFromFile, NextLine As String
RecordNum = 0
Open "C:\TEMP\CENTREX.TXT" For Input As #intOutFile ' Enter path of input file
' code to check if the workbbok is open and if the contents of the workbook are protected
If ActiveWorkbook Is Nothing Then Exit Sub ' no active workbook
If ActiveWorkbook.ProtectStructure Then
MsgBox ActiveWorkbook.Name & " is protected.", vbCritical, "cannot overwrite."
Close intOutFile
Exit Sub
End If
Workbooks("RESULTS.xls").Worksheets("Sheet1").Activate
With ThisWorkbook.Worksheets("sheet1")
Do Until EOF(intOutFile)
Line Input #intOutFile, NextLine
If Mid(NextLine, 1, 3) = "LEN" Then
LinesFromFile = Mid(NextLine, 10, 16)
RecordNum = RecordNum + 1 ' using this code here will give LEN's that are used as BRIs
.Cells(RecordNum, 1) = LinesFromFile
ElseIf Mid(NextLine, 1, 3) = "DN " Then
LinesFromFile = Mid(NextLine, 4, 10)
.Cells(RecordNum, 2) = LinesFromFile
' RecordNum = RecordNum + 1 ' using this code here will eliminate LENs used as BRIs
End If
Loop
End With
Close intOutFile
End Sub
I have the following code that opens a text file searches for certain strings and it works great as long as what I am looking for is in a fixed location but it needs to be expanded to search for a variable that is never in the same location within the string.
sample line 1:
LEN: OKCR 02 0 04 05
sample line 2:
DN 4095557200
What I can't figure out is how to create a search for a third set of characters that moves to different postitions from line to line.
sample line 3:
ACB NOAMA SCWID CND NOAMA CNAMD NOAMA CXR CTALL N STD ACRJ INACT
from sample 3 I need to find each line that contains "CXR" and copy it plus the next 15 characters to column 3 row X.
As I stated "CXR" may be in any position from 1 to 76.
Something I just thought about that is also a problem is that after the "CXR" the remaing data may wrap to the next line (if that is an easy fix then okay but I can live without it).
HERE IS WHAT I HAVE NOW:
Sub Extract_DN_and_LEN_from_a_text_files_WORKS()
'Open a text file and extract data and place it in an Excell spread sheet
'one line at a time
Dim intOutFile As String
intOutFile = FreeFile
Dim RecordNum As Long
Dim LinesFromFile, NextLine As String
RecordNum = 0
Open "C:\TEMP\CENTREX.TXT" For Input As #intOutFile ' Enter path of input file
' code to check if the workbbok is open and if the contents of the workbook are protected
If ActiveWorkbook Is Nothing Then Exit Sub ' no active workbook
If ActiveWorkbook.ProtectStructure Then
MsgBox ActiveWorkbook.Name & " is protected.", vbCritical, "cannot overwrite."
Close intOutFile
Exit Sub
End If
Workbooks("RESULTS.xls").Worksheets("Sheet1").Activate
With ThisWorkbook.Worksheets("sheet1")
Do Until EOF(intOutFile)
Line Input #intOutFile, NextLine
If Mid(NextLine, 1, 3) = "LEN" Then
LinesFromFile = Mid(NextLine, 10, 16)
RecordNum = RecordNum + 1 ' using this code here will give LEN's that are used as BRIs
.Cells(RecordNum, 1) = LinesFromFile
ElseIf Mid(NextLine, 1, 3) = "DN " Then
LinesFromFile = Mid(NextLine, 4, 10)
.Cells(RecordNum, 2) = LinesFromFile
' RecordNum = RecordNum + 1 ' using this code here will eliminate LENs used as BRIs
End If
Loop
End With
Close intOutFile
End Sub