VBA is a programming language and integrated development environment. VBA is included in many of the Microsoft products, including Office. Using the VBA editor programmers can rapidly develop their applications by placing ActiveX controls into their programs. One control often used in VBA to create applications is the ListBox control. The ListBox control is used to display a list of values to users of a VBA program. Working with the ListBox control, programmers can save the ListBox values to a text file on the user's computer. Writing a program that saves values from a ListBox control to your hard drive takes some understanding of VBA and a few lines of code.
- Skill level:
- Moderately Easy
Other People Are Reading
Open Microsoft Excel.
Click "Tools," "Macro" and then click "Visual Basic." This will open the VBA editor.
Click "Insert," "UserForm." You are now in a programming code module.
Select the "ListBox" control from the VBA ToolBox, drag and draw it to your UserForm.
Select "CommandButton" control from the VBA ToolBox, drag and draw it to your UserForm.
Double-click on the CommandButton. This action will take you into the CommandButton's Click event procedure. Type or copy and paste the following code between the "Private Sub CommandButton1_Click()" and "End Sub" procedures.
Your code should look like the following:
Private Sub CommandButton1_Click()
Dim I As Integer
ListBox1.AddItem "This is test 1" ListBox1.AddItem "This is test 2" ListBox1.AddItem "This is test 3" ListBox1.AddItem "This is test 4" Open "C:\list.txt" For Append As #1 For I = 0 To ListBox1.ListCount - 1 Print #1, ListBox1.List(I) Next Close #1
The code adds values to a ListBox object than save the values to a file on your computer call: list.txt under your C: drive.
Press "F5" to run the program. A form should popup click the "Button." Go to your C: drive and look for list.txt, you should see the ListBox's values in that text file.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for