Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default How to read from txt file from bottom to top?

Hi,

I'm using excel 2007 and 2003. I have a routine to loop trough *.txt
files in a folder and search for string in a single line. All was fine
until I had to, based on found value, take all lines from this value
to top, e.g. When I find a line marked with specyfic marker ("in this
case "NC") I have to take all values from over this value until I
reach empty line (so looping backwards), then I continue searching.
Code samples (below function to copy data to specyfic cells)

Function move_all(LineofText, k)
Cells(k, "a") = Mid(LineofText, 2, 2)'Here if
Mid(LineofText, 2, 2) = "NC"
'macro should go back and pull all data until it reaches
empty line.
Cells(k, "b") = Mid(LineofText, 6, 8)
Cells(k, "c") = CLng(Mid(LineofText, 17, 10))
Cells(k, "d") = Str(Mid(LineofText, 29, 15))
Cells(k, "e") = CStr(Mid(LineofText, 46, 38))
Cells(k, "f") = CStr(Mid(LineofText, 86, 4))
Cells(k, "g") = Mid(LineofText, 91, 18)
Cells(k, "h") = Trim(Mid(LineofText, 100, 20))
Cells(k, "i") = Mid(LineofText, 125, 8)
Cells(k, "a").Activate
End Function

Thanks in advance for help

Regards
Piotr
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default How to read from txt file from bottom to top?

A possible start:
Sub ReveseFile()
Dim i1%, LineOfText$, Coll1 As New Collection
Open "c:\autoexec.bat" For Input As #1
Do While Not EOF(1)
i1 = i1 + 1
Line Input #1, LineOfText
Coll1.Add LineOfText, Format(i1)
Loop
Close (1)
Do While i1 0
Debug.Print Coll1(Format(i1))
i1 = i1 - 1
Loop
End Sub ' D-C Dave

Coder1215 wrote:
How to read from txt file from bottom to top?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to read from txt file from bottom to top?

Depending on the file size, you might want to work with it in memory.
Something like



Sub AAA()

Dim TextToMark As String

Dim FileLine As String
Dim FileText As String
Dim Pos As Long
Dim FName As String
Dim FNum As Integer
Dim FileSize As Long
Dim LookFor As String
Dim N As Long
Dim Done As Boolean

Const LINEBREAK = vbCrLf '<<< CHANGE

FName = "C:\Test1\Test.txt" '<<< CHANGE
LookFor = "NC" '<<< CHANGE
FNum = FreeFile()
FileSize = FileLen(FName)

FileText = String$(FileSize, vbNullChar)
Pos = 1

Open FName For Binary Access Read As #FNum

Input #FNum, FileLine
If EOF(FNum) Then
Done = True
End If
Do Until Done
If Len(FileLine) 0 Then
N = InStr(1, FileLine, LookFor, vbTextCompare)
If N 0 Then
If N Len(LookFor) Then
FileLine = Left(FileLine, N - 1)
End If
End If
Mid(FileText, Pos, Len(FileLine) + Len(LINEBREAK)) = FileLine &
LINEBREAK
Pos = Pos + Len(FileLine) + Len(LINEBREAK)
End If
If N 0 Then
Exit Do
End If
If EOF(FNum) = True Then
Done = True
Else
Input #FNum, FileLine
End If
Loop

Close #FNum
TextToMark = Left(FileText, Pos)
Debug.Print TextToMark
End Sub




"Coder1215" wrote in message
...
Hi,

I'm using excel 2007 and 2003. I have a routine to loop trough *.txt
files in a folder and search for string in a single line. All was fine
until I had to, based on found value, take all lines from this value
to top, e.g. When I find a line marked with specyfic marker ("in this
case "NC") I have to take all values from over this value until I
reach empty line (so looping backwards), then I continue searching.
Code samples (below function to copy data to specyfic cells)

Function move_all(LineofText, k)
Cells(k, "a") = Mid(LineofText, 2, 2)'Here if
Mid(LineofText, 2, 2) = "NC"
'macro should go back and pull all data until it reaches
empty line.
Cells(k, "b") = Mid(LineofText, 6, 8)
Cells(k, "c") = CLng(Mid(LineofText, 17, 10))
Cells(k, "d") = Str(Mid(LineofText, 29, 15))
Cells(k, "e") = CStr(Mid(LineofText, 46, 38))
Cells(k, "f") = CStr(Mid(LineofText, 86, 4))
Cells(k, "g") = Mid(LineofText, 91, 18)
Cells(k, "h") = Trim(Mid(LineofText, 100, 20))
Cells(k, "i") = Mid(LineofText, 125, 8)
Cells(k, "a").Activate
End Function

Thanks in advance for help

Regards
Piotr


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default How to read from txt file from bottom to top?

Hi,

I will try the code tomorrow ASAP. The largest file size is over 13
MB, and there are about 30 files in one subfolder, there are 4
subfolders for now and the number will grow each month. The tool is
designed for users to quick fing data they want and put in to excel
making necessarry calculations afted downloading. So the main problem
is speed. I read somewhere that VB6 is a good solution here. Can you
please advise on this?

Thanks very much for help

Regards

Piotr


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to read dates accross bottom of chart Bill43 Charts and Charting in Excel 4 September 13th 07 10:44 AM
I have a read only xl file, I need it to be read and write drama queen Excel Discussion (Misc queries) 3 July 1st 06 12:25 AM
XCEL FILE REC'D AS READ ONLY -- HOW TO NOT BE "READ ONLY" billybob Excel Discussion (Misc queries) 1 February 13th 06 03:14 AM
How can a file be converted from Read-Only to Read/Write Jim in Apopka Excel Discussion (Misc queries) 2 November 19th 05 04:59 PM
"Unable to read file" error message when opening a Excel file that contains a PivotTable report. Tim Marsden Charts and Charting in Excel 2 October 15th 05 02:10 PM


All times are GMT +1. The time now is 04:13 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"