Browse » Home
Tuesday, June 26, 2012
How to Use a ListView Control
1. Open Microsoft Excel.
2. Click 'Tools,' 'Macro' and then 'Visual Basic.' This will open the VBA editor.
3. Click 'Insert' and 'UserForm.' This will place a UserForm on your screen, including the VB ToolBox that contains the controls to use on the UserForm.
4. Add the ListView object onto the ToolBox by clicking 'Tool' and 'Addition Controls.' This opens the ActiveX control box. Scroll down and select 'Microsoft Listview Control' and click 'OK.' This will add the ListView object to the ToolBox.
5. Select the 'ListView' from the ToolBox and draw it onto your UserForm.
6. Double-click on the UserForm. This will open the UserForm's code window.
7. Highlight and select all the content within the Form's code window and press 'Delete' on your keyboard.
8. Type or copy and paste the following code in the code module:Private Sub UserForm_Activate()With ListView1.ListItems.Add.Text = 'Item'.Add.Text = 'Item'.Add.Text = 'Item'.Add.Text = 'Item'.Add.Text = 'Item'.Add.Text = 'Item'.Add.Text = 'Item'.Add.Text = 'Item'End WithEnd SubThe code adds a ListView object onto a UserForm and then uses the ListView's listitems method to populate it with several items. The code is inserted in the UserForm's activate event procedure so the code populates the ListView object instantly at the running of the program.
9. Press 'F5' on your keyboard to run your program; you should have a UserForm on your screen. You should see your ListView object populated with a number of items.