+ Reply to Thread
Results 1 to 15 of 15

Read text file to ListBox and Delete rows in ListBox and re-write text file

  1. #1
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Read text file to ListBox and Delete rows in ListBox and re-write text file

    Hi,

    I am trying to read in a text file to a ListBox and have the ability to delete a row or rows in the listbox and then re-write the text file with the changes. Here is my code that works, but sometimes it deletes the very first row instead of the one I selected from the listbox, but not always. I must have something wrong with my code....

    Please Login or Register  to view this content.
    Thanks for any help you can provide!

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    Nu2Java,

    Without an example file and associated text file, it is difficult to troubleshoot the issue. However, I do see that you have a comment that says "First Item is list" when the ListIndex = 1. That is not the case. The first item in the list is ListIndex = 0
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why do you have this?

    The variable ListIndex isn't used anywhere in the code.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    tigeravatar ... thanks for responding. Attached is my file. The text file I am reading is just a simple line by line history log file with a time stamp. I want to be able to click on a line in the listbox and delete just that line and then update the text file. I have seen this do strange things, sometimes it deletes the one I select and other times it deletes the one I select AND row 1 and 2.

    Thanks for any help!
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    Norie,

    Thanks for responding... I just attached my file. I have been testing various pieces of code and have gotten very similar results.

  6. #6
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    This is really making me crazy I think... I am trying this, but it will just clear the entire listbox
    Please Login or Register  to view this content.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    There's no data.

  8. #8
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    No data where?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    In the workbook that you attached.

    It's kind of hard to help with nothing to work with.

  10. #10
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    All the data is in the VBE. The portion I am having trouble with is reading a text file from "C:Temp" to my UserForm Listbox and then updating the text file with the contents left from the listbox.

    Text File looks like:

    Please Login or Register  to view this content.
    Read that into ListBox and delete the last line "Line 4" but the code just deletes all lines.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    The only data in the VBE is for the Assembly combobox.
    Please Login or Register  to view this content.
    If the data for the listbox is in a file, can you attach that file?

  12. #12
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    I will have to check the file when I return home and re-post it. My problem is I cannot read even a 2 line text file and delete line 2 in the listbox and then re-write the text file with line 1. It seems to clear the entire text file and returns "Null"

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    It sounds like the problem could be with the reading/writing from/to the form.

    When/where is the listbox populated?

    There is a sub called cmbNotes_Click which has code to populate the listbox, apparently from a file, but there's no control called cmbNotes.

  14. #14
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    I think you are right... I will play with this more and re-post. I guess I'm just not sure how to update a text file from a listbox.

  15. #15
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Read text file to ListBox and Delete rows in ListBox and re-write text file

    Ok, I got this working to be able to delete the last line of the text file and it seems to be re-writing the text file properly. The only problem with it is that I cannot select a line, which is what I would really like to do. Here is my code:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1