Using FSO method not able to retrieve the line numbers

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
688
Hi

Using FSO i would like to determine the LineNumber of Text File if the Following examples as per 3 cases
The below cases demonstrates the line nos and are the Last Lines of Text File
The line Nos can be viewed in Notepad ++

Case 1

1784
1785 The Brown Fox
1786 Quickly Jumped over
1787 The Lazy Dogs
1788 # This is the Cursor Blinking on the Last Blank Line 1788
1789
....
....

Case 2
1784
1785 The Brown Fox
1786 Quickly Jumped over
1787 The Lazy Dogs# This is the Cursor Blinking on the Last Non-Empty/Blank Line 1787
1788
1789
....
....


Case 3
1784
1785 The Brown Fox
1786 Quickly Jumped over
1787 The Lazy Dogs
1788
1789
1790
1791
1792
1793
1794# This is the Cursor Blinking on the Last Blank Line 1794
....
....
As per Case 1 I would like to have Msg Displayed as Total Lines :1788 The Last Non Empty Line is Line no. 1787 and The Last Blank Line No. is 1788

As per Case 2 I would like to have Msg Displayed as Total Lines :1787 The Last And Non Empty Line is Line no. 1787

As per Case 3 I would like to have Msg Displayed as Total Lines :1794 The Last Non Empty Line is Line no. 1787 and The Last Blank Line No. is 1794

Below is the code for above result only was able to achieve the result of Case 2. FYI

VBA Code:
Private Sub FsoMethod()
Dim objfso As Object, ObjFile As Object, lCount As Long
Set objfso = CreateObject("Scripting.FileSystemObject")

Dim FileName As String

Dim strAllData As String
Dim StrLines() As String


Dim ArrFileLines() As Variant
Dim LstLineNo As Long
Dim FileNum As Integer
Dim LastNonBlankLineNo As Long
Dim i As Long, ln As Integer

Dim StrLinesData As String
Dim strLastLine As String, strline As String, strLinenumber As Long, totLines As Long

Dim LastEmptyLineNo As Integer

Dim msgEmpty As String, msgNonEmpty As String

FileName = "C:\ABC\Trial.txt"
FileNum = FreeFile
LstLineNo = 1


Const ForReading = 1

Set ObjFile = objfso.OpenTextFile(FileName, ForReading)

i = 0

Do While Not ObjFile.AtEndOfStream
    strLastLine = ObjFile.ReadLine
    totLines = ObjFile.Line
 
    If Len(strLastLine) > 0 Then
         StrLinesData = strLastLine
         LastEmptyLineNo = ObjFile.Line
    End If
 
Loop

If totLines = LastEmptyLineNo Then
               msgEmpty = "The Last And Non Empty Line is Line no."
               strAllData = "TOTAL LINES : " & totLines & "  " & msgEmpty & LastEmptyLineNo  
End If

If totLines > LastEmptyLineNo Then
          LastEmptyLineNo = totLines - LastEmptyLineNo     
          msgEmpty = " The Last Non Empty Line is Line no.  "
          strAllData = "TOTAL LINES : " &  totLines  & "  " & StrLinesData  &  msgEmpty & LastEmptyLineNo &  " The last Blank Line is "
End If

TextBox2.Text = strAllData

End Sub
Your help will be indeed appreicated
Thanks and Regard
NimishK
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Below is the code for above result only was able to achieve the result of Case 2. FYI
Can you please provide a bit more information. When you say that it only worked for Case 2, what happens with the other cases? Is the number wrong? Does it provide a number at all? Is there an error message? If so, what's the error number?
 
Upvote 0
Thanks Dan_W for reverting back
Can you please provide a bit more information. When you say that it only worked for Case 2, what happens with the other cases? Is the number wrong? Does it provide a number at all? Is there an error message? If so, what's the error number?
Result of Case 1
Total Lines :1788 The Lazy Dogs The Last Non Empty Line is Line no. 1788 and The Last Blank Line No. is
the above result is wrong The Last Non Empty Line is Line no. 1788 instead of 1787


Result of Case 3
Total Lines :1794 The Lazy Dogs The Last Non Empty Line is Line no. 1788 and The Last Blank Line No. is
the above result is wrong The Last Non Empty Line is Line no. 1788 instead of 1787

I modifed this part of the code for above results
VBA Code:
If totLines > LastEmptyLineNo Then
         msgEmpty = " The Last Non Empty Line is Line no. "
strAllData = "Total Lines : " & totLines & StrLinesData & msgEmpty & LastEmptyLineNo & " and The Last Blank Line No. is "
End If
i've even coloured the respective results for more clarity

NimishK
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top