Guest User

Untitled

a guest
Feb 23rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. # ListView control in Excel spreadsheets
  2.  
  3. List view controls can be exceptionally useful for viewing data on a spreadsheet.
  4.  
  5. ## Step 1: Add control to spreadsheet.
  6.  
  7. 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.
  8.  
  9. ## Step 2: Find ListView control
  10.  
  11. Scroll down through the `More Controls` dialogue until you find `Microsoft ListView Control, version 6.0`.
  12.  
  13. ## Step 3: Draw the ListView control
  14.  
  15. Draw the list view control on the Excel spreadsheet.
  16.  
  17. ## Step 4: Change ListView properties
  18.  
  19. Right click on the ListView control. In the context menu click on the `Properties` button in the `ListViewCtrl Object` menu.
  20.  
  21. > 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.
  22.  
  23. When here we can start to change the list view properties. The list view properties we will likely want are:
  24.  
  25. ### `General` tab
  26.  
  27. ```
  28. View: 3-lvwReport
  29. HideSelection: false
  30. MultiSelect: true
  31. AllowColumnReorder: true
  32. FullRowSelect: true
  33. GridLines: true
  34. ```
  35.  
  36. ### `Column Headers` tab
  37.  
  38. 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.
  39.  
  40. ### Finally
  41.  
  42. Apply the changes and close the properties dialog. Give this listview a name! This will be used later to fill the listview with data!
  43.  
  44. ## Fill the listview with data
  45.  
  46. Now that the list view has been created, we can now fill it with data.
  47.  
  48. ```vbs
  49. sub fillData(sh as Worksheet, ByVal sLVName as string)
  50. 'Get object
  51. Dim lv as Object, lvi as
  52. set lv = sh.OLEObjects(sLVName).Object
  53.  
  54. 'Clear existing data
  55. lv.ListItems.Clear
  56.  
  57. 'Add data
  58. Dim lvi as object
  59.  
  60. 'Row 1
  61. Set lvi = ListView.ListItems.Add()
  62. lvi.Text = "Row_1_1"
  63. lvi.ListSubItems.Add().Text = "Row_1_2"
  64. lvi.ListSubItems.Add().Text = "Row_1_3"
  65.  
  66. 'Row 2
  67. Set lvi = ListView.ListItems.Add()
  68. lvi.Text = "Row_2_1"
  69. lvi.ListSubItems.Add().Text = "Row_2_2"
  70. lvi.ListSubItems.Add().Text = "Row_2_3"
  71.  
  72. 'Row 3
  73. Set lvi = ListView.ListItems.Add()
  74. lvi.Text = "Row_3_1"
  75. lvi.ListSubItems.Add().Text = "Row_3_2"
  76. lvi.ListSubItems.Add().Text = "Row_3_3"
  77.  
  78. 'Row 4
  79. Set lvi = ListView.ListItems.Add()
  80. lvi.Text = "Row_4_1"
  81. lvi.ListSubItems.Add().Text = "Row_4_2"
  82. lvi.ListSubItems.Add().Text = "Row_4_3"
  83.  
  84. 'Row 5
  85. Set lvi = ListView.ListItems.Add()
  86. lvi.Text = "Row_5_1"
  87. lvi.ListSubItems.Add().Text = "Row_5_2"
  88. lvi.ListSubItems.Add().Text = "Row_5_3"
  89. end sub
  90. ```
Add Comment
Please, Sign In to add comment