+ Reply to Thread
Results 1 to 15 of 15

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

Hybrid View

Nu2Java Read text file to ListBox and... 10-26-2012, 09:32 AM
tigeravatar Re: Read text file to ListBox... 10-26-2012, 11:47 AM
Norie Why do you have this? The... 10-26-2012, 12:40 PM
Nu2Java Re: Read text file to ListBox... 10-26-2012, 12:49 PM
Nu2Java Re: Read text file to ListBox... 10-26-2012, 12:51 PM
Nu2Java Re: Read text file to ListBox... 10-26-2012, 03:29 PM
Norie Re: Read text file to ListBox... 10-26-2012, 03:38 PM
Nu2Java Re: Read text file to ListBox... 10-26-2012, 03:43 PM
Norie Re: Read text file to ListBox... 10-26-2012, 03:47 PM
Nu2Java Re: Read text file to ListBox... 10-26-2012, 03:53 PM
Norie Re: Read text file to ListBox... 10-26-2012, 04:00 PM
Nu2Java Re: Read text file to ListBox... 10-26-2012, 04:14 PM
Norie Re: Read text file to ListBox... 10-26-2012, 04:21 PM
Nu2Java Re: Read text file to ListBox... 10-26-2012, 05:01 PM
Nu2Java Re: Read text file to ListBox... 10-29-2012, 09:42 AM
  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....

    Private Sub CommandButton2_Click()
    
    ListIndex = 1 'First Item is list
    ListBox1.RemoveItem (ListBox1.ListIndex)
    
    MyFile = "c:\temp2\" & txtCCA & ".txt"
    
    Open MyFile For Output As #1:
    Print #1, Me.ListBox1.Value
    
    Close #1
    Me.txtComments.Value = vbNullString
    End Sub
    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,644
    Why do you have this?

    The variable ListIndex isn't used anywhere in the code.
    ListIndex = 1
    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
    ListBox1.RemoveItem (ListBox1.ListCount - 1)

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

    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,644

    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:

    Line 1
    Line 2
    Line 3
    Line 4
    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,644

    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.
    txtCCA.List = Array("80-777003-04", "8-879023-01", "8-736604-01")
    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,644

    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:

    Private Sub CommandButton2_Click()
    
    ListBox1.RemoveItem (ListBox1.ListCount - 1)
    
    MyFile2 = "c:\temp2\" & txtCCA & ".txt"
    
    fnum2 = FreeFile()
    Open MyFile2 For Output As fnum2:
    For i = 0 To ListBox1.ListCount - 1
    Print #fnum2, ListBox1.Column(0, i)
    Next
    
    Close #fnum2
    Me.txtComments.Value = vbNullString
    End Sub

+ 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