Skip to content

Instantly share code, notes, and snippets.

@sancarn
Last active February 10, 2018 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sancarn/2066efe6323156a9a3e7040c3a204df2 to your computer and use it in GitHub Desktop.
Save sancarn/2066efe6323156a9a3e7040c3a204df2 to your computer and use it in GitHub Desktop.

ListView control in Excel spreadsheets

List view controls can be exceptionally useful for viewing data on a spreadsheet.

Step 1: Add control to spreadsheet.

Click on the Insert dropdown button in the Controls group of the Developer tab. When here click the More controls button of the ActiveX Controls sub menu.

Step 2: Find ListView control

Scroll down through the More Controls dialogue until you find Microsoft ListView Control, version 6.0.

Step 3: Draw the ListView control

Draw the list view control on the Excel spreadsheet.

Step 4: Change ListView properties

Right click on the ListView control. In the context menu click on the Properties button in the ListViewCtrl Object menu.

Note: You will only be able to access this menu in Design Mode which you will be able to access from the Controls group in the Developer tab.

When here we can start to change the list view properties. The list view properties we will likely want are:

General tab

View:               3-lvwReport
HideSelection:      false
MultiSelect:        true
AllowColumnReorder: true
FullRowSelect:      true
GridLines:          true

Column Headers tab

Here click Insert Column until you have the correct number of columns. For each column you can change the Text property (the text displayed in the column header) Key is the Key property in VBA. All other properties you can likely ignore.

Finally

Apply the changes and close the properties dialog. Give this listview a name! This will be used later to fill the listview with data!

Fill the listview with data

Now that the list view has been created, we can now fill it with data.

sub fillData(sh as Worksheet, ByVal sLVName as string)
  'Get object
  Dim lv as Object, lvi as 
  set lv = sh.OLEObjects(sLVName).Object
  
  'Clear existing data
  lv.ListItems.Clear
  
  'Add data
  Dim lvi as object
  
  'Row 1
  Set lvi = ListView.ListItems.Add()  
  lvi.Text = "Row_1_1"
  lvi.ListSubItems.Add().Text = "Row_1_2"
  lvi.ListSubItems.Add().Text = "Row_1_3"
  
  'Row 2
  Set lvi = ListView.ListItems.Add()  
  lvi.Text = "Row_2_1"
  lvi.ListSubItems.Add().Text = "Row_2_2"
  lvi.ListSubItems.Add().Text = "Row_2_3"
  
  'Row 3
  Set lvi = ListView.ListItems.Add()  
  lvi.Text = "Row_3_1"
  lvi.ListSubItems.Add().Text = "Row_3_2"
  lvi.ListSubItems.Add().Text = "Row_3_3"
  
  'Row 4
  Set lvi = ListView.ListItems.Add()  
  lvi.Text = "Row_4_1"
  lvi.ListSubItems.Add().Text = "Row_4_2"
  lvi.ListSubItems.Add().Text = "Row_4_3"
  
 'Row 5
  Set lvi = ListView.ListItems.Add()  
  lvi.Text = "Row_5_1"
  lvi.ListSubItems.Add().Text = "Row_5_2"
  lvi.ListSubItems.Add().Text = "Row_5_3"
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment