Results 1 to 10 of 10

Thread: Read text File into listbox in reverse order

  1. #1
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Read text File into listbox in reverse order

    I have some lines in a text file. the lines were written in a way such that the last line is added at the bottom. i have a listbox that i want it to read the lines in the text file in a reverse order i.e from the last line to the first line

  2. #2
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,239
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: Read text File into listbox in reverse order

    Quote Originally Posted by yinkajewole View Post
    I have some lines in a text file. the lines were written in a way such that the last line is added at the bottom. i have a listbox that i want it to read the lines in the text file in a reverse order i.e from the last line to the first line
    Assuming your ListBox is on a UserForm and that it is named ListBox1 and that you change the red highlighted text to your actual path/filename, run this subroutine from whatever event (initialize, button click, etc.) you want...
    Code:
    Sub ReverseTextFileShowInListBox()
      Dim X As Long, FileNum As Long, TotalFile As String, Lines As Variant
      FileNum = FreeFile
      Open "C:\Temp\ReverseMe.txt" For Binary As #FileNum 
        TotalFile = Space(LOF(FileNum))
        Get #FileNum , , TotalFile
      Close #FileNum 
      Lines = Split(TotalFile, vbCrLf)
      For X = UBound(Lines) To 0 Step -1
        ListBox1.AddItem Lines(X)
      Next
    End Sub
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  3. #3
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read text File into listbox in reverse order

    I'm not with my pc at the moment, i'll try it later.
    by the way, is the possible to list them by eliminating duplicates?

  4. #4
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,239
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: Read text File into listbox in reverse order

    Quote Originally Posted by yinkajewole View Post
    I'm not with my pc at the moment, i'll try it later.
    by the way, is the possible to list them by eliminating duplicates?
    That will take different code as this is basically a different question. However, you need to clarify something for us. Since you are asking for the list to be reversed, the question comes to mind which duplicate should be kept... the first one on the existing list or the last one? For example, if this is your list...

    nine
    eight
    three
    seven
    six
    three
    three
    five
    four
    three
    two
    one

    would you want the reversed list to be this...

    one
    two
    four
    five
    six
    seven
    three
    eight
    nine

    or this...

    one
    two
    three
    four
    five
    six
    seven
    either
    nine
    Last edited by Rick Rothstein; May 5th, 2019 at 02:55 PM.
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  5. #5
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read text File into listbox in reverse order

    the last one on the existing list i.e
    jack
    john
    peter
    lord
    john --> this should be retained
    cole

  6. #6
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read text File into listbox in reverse order

    the list should be reversed like this...
    one
    two
    four
    five
    six
    seven
    three
    eight
    nine

  7. #7
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,239
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: Read text File into listbox in reverse order

    Quote Originally Posted by yinkajewole View Post
    the list should be reversed like this...
    one
    two
    four
    five
    six
    seven
    three
    eight
    nine
    Try using this code in place of the code I gave you earlier...
    Code:
    Sub ReverseTextFileShowInListBox()
      Dim X As Long, FileNum As Long
      Dim TotalFile As String, InListBox As String
      Dim Lines As Variant
      FileNum = FreeFile
      Open "C:\Temp\ReverseMe.txt" For Binary As #FileNum 
        TotalFile = Space(LOF(FileNum))
        Get #FileNum , , TotalFile
      Close #FileNum 
      Lines = Split(TotalFile, vbCrLf)
      With CreateObject("Scripting.Dictionary")
        For X = UBound(Lines) To 0 Step -1
          If .Exists(Lines(X)) Then .Remove Lines(X)
          .Item(Lines(X)) = 1
        Next
        ListBox1.List = .Keys
      End With
    End Sub
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  8. #8
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,228
    Post Thanks / Like
    Mentioned
    69 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Read text File into listbox in reverse order

    Only another way

    Code:
    Private Sub CommandButton1_Click()
      Workbooks.OpenText Filename:="C:\trabajo\otro.txt", Origin:=xlWindows, FieldInfo:=Array(1, 1)
      Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).RemoveDuplicates Columns:=1, Header:=xlYes
      Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row) = "=INDIRECT(""A"" & " & Range("A" & Rows.Count).End(xlUp).Row + 1 & "-ROW())"
      ListBox1.List = Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row).Value
      ActiveWorkbook.Close False
    End Sub
    Regards Dante Amor

  9. #9
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read text File into listbox in reverse order

    Quote Originally Posted by Rick Rothstein View Post
    Try using this code in place of the code I gave you earlier...
    Code:
    Sub ReverseTextFileShowInListBox()
      Dim X As Long, FileNum As Long
      Dim TotalFile As String, InListBox As String
      Dim Lines As Variant
      FileNum = FreeFile
      Open "C:\Temp\ReverseMe.txt" For Binary As #FileNum 
        TotalFile = Space(LOF(FileNum))
        Get #FileNum , , TotalFile
      Close #FileNum 
      Lines = Split(TotalFile, vbCrLf)
      With CreateObject("Scripting.Dictionary")
        For X = UBound(Lines) To 0 Step -1
          If .Exists(Lines(X)) Then .Remove Lines(X)
          .Item(Lines(X)) = 1
        Next
        ListBox1.List = .Keys
      End With
    End Sub
    great it worked

  10. #10
    Board Regular
    Join Date
    Nov 2018
    Posts
    202
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read text File into listbox in reverse order

    @Rick Rothstein I don't know if I'm asking for too much, I'm sorry, however, I believe you can help if it's possible.

    I just had an idea.
    using the code in this thread to write the file name and path in a text file https://www.mrexcel.com/forum/excel-...text-file.html
    how possible is it to use your code to list the file names and path in the text file into the listbox so that it would hide the path but show only the names?

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •