Fredric,
I've altered your code a little bit. I added a Case statement instead of
your If, ElseIf statements. (I think that the Case statement is a bit easier
to read. If you haven't used a Case statement before, the VBE Help
documentation has some good information on how it works. It acts very
similar to the If...Then statement. Also, take note of the comments listed
in the code below).
I'm still not 100% sure what you are trying to achieve. What your code is
set up to do is the following:
1. Test for "CEG_HEADER". If it exists, do something and move to the next
line of the text file.
2. If "CEG_HEADER" does NOT exist, test for "FILENAME". If "FILENAME"
exists, do something and move to the next line of the text file.
3. If "FILENAME" does NOT exist, test for "INTRCHGHDR". Etc.
....
If "CEG_HEADER", "FILENAME", "INTRCHGHDR", "RECIPNTDTL", "SPRPRODHDR",
"SPRCONTBTN", AND "PAYDETAILS" do NOT exist, insert the "----".
Thus, I don't exactly know what you mean by "If the word ["PAYDETAILS"]
isn't there then in the worksheet I need to place 'N/A'," as noted in your
first post. I say this because, as noted above, if "PAYDETAILS" does NOT
exist (meaning that none of the other words exist either and the code has
reached the "Case Else" section), then insert the "----".
Are you familiar with the Debugging tools inside VBE? If not, then at least
use the following to test and evaluate your code:
In the "Debug" menu, there is a "Step Into F8" option. Step Into will allow
you to evaluate your code one line at a time. As you evaluate your code
(i.e. press the F8 key) you will see a yellow line highlight syntax. As you
press F8, you will see the yellow line advance one line at a time. I think
that doing this simple exercise will help you see how your logic is behaving
and will hopefully help you see where/why the logic is not behaving as you
anticipated. Otherwise, you'll have to really spell out what you are looking
for.
For example, I don't know what you mean or intend when you say "...the
information retreived made only 4 rows on the worksheet, [but] columns L, or
M, or N used up 57 rows." Do you mean that columns L, or M, or N should have
used up only 2 rows (i.e. the two "CHQ" lines from your post should be on 2
rows only instead of spread out and separated by "----"?). How is the data
supposed to look? (For example, assuming from your post that the dollar
column is "A" and the subsequent columns move in alphabetical order [i.e. A1
= $773.75, B1 = 17092009, C1 = SUPERLIFE PTY, D1 = ----, E1 = ----, F1 =
----, etc. ] there should be the four rows of data (A1:C4) as shown in the
post, but E1 = CHQ, F1 = 12082009, G1 = $77.37; E2 = CHQ, F2 = 12082009, G2 =
$101.59; F3:G4 = ----).
Best,
Matt
Sub ReadText()
Dim strTextLine As String
Dim strFilename As String
Dim strNewFilepath As String
Dim intFileHandle As Integer
Dim Wks As Worksheet
Dim lngNextRow As Long
'should this file have a ".txt" extension? If so, then
' you'll have to adjust the suggestion for strNewFilepath
' listed below
strFilename = "C:\Russell PSGCD.XFR.GRPN.D130809.F.S00570"
If Dir(strFilename) = "" Then
MsgBox "File Not Found"
Exit Sub
End If
intFileHandle = FreeFile
'consider the following instead, which finds the first space,
' working right to left, in order to get the "new" filename:
'strNewFilepath = Right(strFilename, Len(strFilename) -
InStrRev(strFilename, " "))
strNewFilepath = Right(strFilename, 31)
Open strFilename For Input As intFileHandle
Set Wks = ThisWorkbook.Worksheets("ECI File Index")
With Wks
Do While Not EOF(intFileHandle)
Line Input #intFileHandle, strTextLine
Select Case Left(strTextLine, 10)
Case "CEG_HEADER"
lngNextRow = .Range("O" & .Rows.Count).End(xlUp).Row + 1
.Range("O" & lngNextRow).Value = Mid(strTextLine, 40, 77)
Case "FILENAME"
lngNextRow = .Range("C" & .Rows.Count).End(xlUp).Row + 1
.Range("C" & lngNextRow).Value = Mid(strTextLine, 11, 44)
Case "INTRCHGHDR"
lngNextRow = .Range("D" & .Rows.Count).End(xlUp).Row + 1
.Range("D" & lngNextRow).Value = Mid(strTextLine, 148, 10)
lngNextRow = .Range("E" & .Rows.Count).End(xlUp).Row + 1
.Range("E" & lngNextRow).Value = Mid(strTextLine, 191, 8)
Case "RECIPNTDTL"
lngNextRow = .Range("K" & .Rows.Count).End(xlUp).Row + 1
.Range("K" & lngNextRow).Value = Mid(strTextLine, 11, 76)
Case "SPRPRODHDR"
lngNextRow = .Range("G" & .Rows.Count).End(xlUp).Row + 1
.Range("G" & lngNextRow).Value = Mid(strTextLine, 31, 11)
lngNextRow = .Range("H" & .Rows.Count).End(xlUp).Row + 1
.Range("H" & lngNextRow).Value = Mid(strTextLine, 51, 76)
With .Range("H" & lngNextRow)
If .Offset(0, 7) = "" Then
.Offset(0, 7).Value = "-----"
'double check this Offset
.Offset(0, -6).Value = "--"
'double check this Offset
.Offset(0, -7).Value = strNewFilepath
ElseIf .Offset(0, 7) <> "-----" And .Offset(0, 7) <> ""
Then
.Offset(0, -6).Value = "Y"
'double check this Offset
.Offset(0, -7).Value = strNewFilepath
End If
End With
Case "SPRCONTBTN"
lngNextRow = .Range("I" & .Rows.Count).End(xlUp).Row + 1
.Range("I" & lngNextRow).Value = Mid(strTextLine, 11, 13)
lngNextRow = .Range("J" & .Rows.Count).End(xlUp).Row + 1
.Range("J" & lngNextRow).Value = Mid(strTextLine, 40, 8)
Case "PAYDETAILS"
lngNextRow = .Range("L" & .Rows.Count).End(xlUp).Row + 1
.Range("L" & lngNextRow).Value = Mid(strTextLine, 11, 5)
lngNextRow = .Range("M" & .Rows.Count).End(xlUp).Row + 1
.Range("M" & lngNextRow).Value = Mid(strTextLine, 16, 8)
lngNextRow = .Range("N" & .Rows.Count).End(xlUp).Row + 1
.Range("N" & lngNextRow).Value = Mid(strTextLine, 24, 12)
Case Else
lngNextRow = .Range("L" & .Rows.Count).End(xlUp).Row + 1
.Range("L" & lngNextRow).Value = "-----"
lngNextRow = .Range("M" & .Rows.Count).End(xlUp).Row + 1
.Range("M" & lngNextRow).Value = "-----"
lngNextRow = .Range("N" & .Rows.Count).End(xlUp).Row + 1
.Range("N" & lngNextRow).Value = "-----"
End Select
Loop
End With
Close intFileHandle
End Sub
bluewatermist said:
Hi Matthew
This problem was my first original question, but I have probably not stated
it clearly.
The text files have lots of other information which I don't require. What
is happening with the macro below is that if for example the macro doesn't
find on the next line the word "PAYDETAILS" it places "-----" on column L, or
M, or N. Therefore if the information retreived made only 4 rows on the work
sheet, columns L, or M or N used up 57 rows. I have place an example below
and the code.
Hope i have made it clearer
Frederic
$773.75 17092009 SUPERLIFE PTY ----- ----- -----
$1015.90 17092009 SUPERLIFE PTY ----- ----- -----
$315.00 11092009 SUPERLIFE PTY ----- ----- -----
$142.50 17092009 SUPERLIFE PTY ----- ----- -----
----- ----- -----
----- ----- -----
----- ----- -----
----- ----- -----
CHQ 12082009 $77.37
----- ----- -----
----- ----- -----
----- ----- -----
----- ----- -----
----- ----- -----
----- ----- -----
----- ----- -----
CHQ 12082009 $101.59
----- ----- -----
----- ----- -----
Sub ReadText()
Dim strTextLine As String
Dim strFilename As String
Dim vFileHandle As Integer
Dim Wks As Worksheet
strFilename = "C:\Russell PSGCD.XFR.GRPN.D130809.F.S00570"
If Dir(strFilename) = "" Then
MsgBox ("File Not Found")
Exit Sub
End If
vFileHandle = FreeFile
newfilepath = Right(strFilename, 31)
Open strFilename For Input As vFileHandle
Do While Not EOF(vFileHandle)
Line Input #vFileHandle, strTextLine
Set Wks = ThisWorkbook.Worksheets("ECI File Index")
If InStr(1, Left(strTextLine, 10), "CEG_HEADER") > 0 Then
With Wks
NextRow = Range("O65536").End(xlUp).Row + 1
Range("O" & NextRow).Select
ActiveCell = Mid(strTextLine, 40, 77)
End With
ElseIf InStr(1, Left(strTextLine, 10), "FILENAME") > 0 Then
With Wks
NextRow = Range("C65536").End(xlUp).Row + 1
Range("C" & NextRow).Select
ActiveCell = Mid(strTextLine, 11, 44)
End With
ElseIf InStr(1, Left(strTextLine, 10), "INTRCHGHDR") > 0 Then
With Wks
NextRow = Range("D65536").End(xlUp).Row + 1
Range("D" & NextRow).Select
ActiveCell = Mid(strTextLine, 148, 10)
NextRow = Range("E65536").End(xlUp).Row + 1
Range("E" & NextRow).Select
ActiveCell = Mid(strTextLine, 191, 8)
End With
ElseIf InStr(1, Left(strTextLine, 10), "RECIPNTDTL") > 0 Then
With Wks
NextRow = Range("K65536").End(xlUp).Row + 1
Range("K" & NextRow).Select
ActiveCell = Mid(strTextLine, 11, 76)
End With
ElseIf InStr(1, Left(strTextLine, 10), "SPRPRODHDR") > 0 Then
With Wks
NextRow = Range("G65536").End(xlUp).Row + 1
Range("G" & NextRow).Select
ActiveCell = Mid(strTextLine, 31, 11)
NextRow = Range("H65536").End(xlUp).Row + 1
Range("H" & NextRow).Select
ActiveCell = Mid(strTextLine, 51, 76)
If ActiveCell.Offset(0, 7) = "" Then
ActiveCell.Offset(0, 7).Select
Selection.Value = "-----"
ActiveCell.Offset(0, -13).Select
Selection.Value = "--"
ActiveCell.Offset(0, -1).Select
Selection.Value = newfilepath
ElseIf ActiveCell.Offset(0, 7) <> "-----" And ActiveCell.Offset(0, 7) <> ""
Then
ActiveCell.Offset(0, -6).Select
Selection.Value = "Y"
ActiveCell.Offset(0, -1).Select
Selection.Value = newfilepath
End If
End With
ElseIf InStr(1, Left(strTextLine, 10), "SPRCONTBTN") > 0 Then
With Wks
NextRow = Range("I65536").End(xlUp).Row + 1
Range("I" & NextRow).Select
ActiveCell = Mid(strTextLine, 11, 13)
NextRow = Range("J65536").End(xlUp).Row + 1
Range("J" & NextRow).Select
ActiveCell = Mid(strTextLine, 40, 8)
End With
ElseIf InStr(1, Left(strTextLine, 10), "PAYDETAILS") > 0 Then
With Wks
NextRow = Range("L65536").End(xlUp).Row + 1
Range("L" & NextRow).Select
ActiveCell = Mid(strTextLine, 11, 5)
NextRow = Range("M65536").End(xlUp).Row + 1
Range("M" & NextRow).Select