Excel

Read Text File into ListBox

Ease of Use

Intermediate

Version tested with

2000, 2002 

Submitted by:

Justinlabenne

Description:

An easy way to read a text file's contents into a listbox on a Userform. 

Discussion:

Can be used to read a License agreement or ReadMe file into a listbox on a userform for the user to view. Also read textfile's full of data into the listbox and then process lines that are needed instead of importing an entire file to a worksheet. Lots of possiblities as to uses. The attached example reads a "ReadMe" file into the listbox on the form. It is included in the example. 

Code:

instructions for use

			

Option Explicit ' ================================================================= ' Text file contents must have line breaks in certain spots so it ' can be read into the listbox correctly. It takes some trial and ' error to figure on the formatting correctly so the contents can ' all be read. ' ================================================================= Private Sub UserForm_Initialize() ' Clear the list box and caption With Me .lstText.Clear .lblFilePath.Caption = Empty End With End Sub Private Sub cmdRun_Click() ' Text files name Const szFileName As String = "ReadMe.txt" ' Clear the list box and caption With Me .lstText.Clear .lblFilePath.Caption = Empty End With ' ======================================================= ' Variables for creating a path that the ' text file is expected to be in Dim szThisPath As String szThisPath = ThisWorkbook.Path Dim szPathSep As String szPathSep = Application.PathSeparator Dim szValidPath As String szValidPath = szThisPath & szPathSep & szFileName ' ======================================================= ' Place the files name and path location into a label, just ' so you remember where your file came from. Relevant if your ' going to incorporate a browse for file routine Me.lblFilePath.Caption = "Reading file from: " & szValidPath ' We need an Error handler just in case the file we hard-coded is ' missing, renamed, etc... On Error GoTo ErrHandler ' ======================================================= ' Read each line from the text file, adding it to the ' list box as an item Dim lFile As Long Dim szLine As String lFile = FreeFile() Open szValidPath For Input As lFile While Not EOF(lFile) Line Input #lFile, szLine Me.lstText.AddItem szLine Wend Close lFile ' ======================================================= Exit Sub ErrHandler: Me.lblFilePath.Caption = Empty MsgBox Err.Description End Sub Private Sub lstText_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) ' De-select current items Dim i As Long For i = 0 To Me.lstText.ListCount - 1 Me.lstText.Selected(i) = False Next i End Sub Private Sub cmdClose_Click() ' Close the form Unload Me End Sub 'In standard module Sub ShowMyForm() frmGetTxt.Show End Sub

How to use:

  1. Open an Excel Workbook
  2. Copy the code
  3. Press Alt + F11 to open the Visual Basic Editor (VBE)
  4. Select INSERT > USERFORM from the menubar
  5. Rename the userform: frmGetTxt
  6. You will need to add:
  7. 1 Label = lblFilePath
  8. 2 CmdButtons = cmdRun & cmdClose
  9. 1 Listbox = lstText
  10. Double Click on one of the buttons
  11. Paste code into the userform code module
  12. Select INSERT > MODULE from the menubar
  13. Paste code into the right pane
  14. Copy the {ShowMyForm} code into the standard module
  15. Press Alt+Q to return to Excel
  16. Save workbook before any other changes
  17. Create a text file in the same location as the Excle workbook
  18. Name it {ReadMe} with a .txt extension
  19. Add any kind of content to it you like.
 

Test the code:

  1. Go to TOOLS > MACRO > MACROS
  2. Select {ShowMyForm} from the dialog box
  3. Press {Run}
  4. The userform will be displayed
  5. Press the {Run} button on teh form to read the text files contents to the listbox
 

Sample File:

TextIntoList.zip 13.1KB 

Approved by mdmackillop


This entry has been viewed 487 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express