{"id":12132,"date":"2012-03-25T20:27:00","date_gmt":"2012-03-25T20:27:00","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1538"},"modified":"2022-08-07T00:14:21","modified_gmt":"2022-08-07T00:14:21","slug":"excel-macro-tutorial-how-to-use-listbox-in-excel","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/03\/excel-macro-tutorial-how-to-use-listbox-in-excel\/","title":{"rendered":"Excel Macro Tutorial : How to Use ListBox in Excel"},"content":{"rendered":"
As we saw in the Previous Article you learnt How to use Drop Down Box in Excel<\/strong><\/a> In this Article you will learn How to Use ListBox in Excel.<\/strong> It will include Excel Macro Tutorial – Add ListBox 1<\/p><\/div>\n Now Drag and Drop that Control any where you want in your Spreadsheet. Excel Macro Tutorial – ListBox – Design Mode<\/p><\/div><\/p>\n Now as you can see that ListBox is created in the Sheet. Now you will learn How to Add Options or Items in the Drop down.<\/strong> As of Now it is completely blank. There is no option\/item in it. To Add items by setting Properties of the ListBox, follow the below Steps: Excel Macro Tutorial – Add ListBox – Properties<\/p><\/div><\/p>\n Now whatever List Items you want to be there in the ListBox, Type them in that Range in Excel Sheet. All the list available in that Range will start appearing in the List Box as shown below: Excel Macro Tutorial – Add ListBox 2<\/p><\/div><\/p>\n You might have in Visual Studio, we have separate Controls Like CheckBox List or RadioButton List Box. <\/p>\n How to Allow to Select More than One Item in List Box(List Of CheckBoxes): <\/strong> Excel Macro Tutorial – Add Radio Button List Box<\/p><\/div>\n How to Allow to Select Only One Item from List Box (List Of Radio Buttons): <\/strong><\/p>\n To make this ListBox as a List of CheckBoxes, you need to change the below properties of the List Box: Excel Macro Tutorial – Add Check List Box<\/p><\/div>\n In List Box, all the Items are by default Indexed from 0, 1, 2 etc. The first item which is added to ListBox will be indexed as 0, Second One will be Indexed as 1 and so on.. Therefore no need of any indexing here while adding list items in the ListBox. In the Above Code, Sheet1<\/strong> is the Sheet Name where ListBox is there and ListBox1<\/strong> is the Name of the ListBox Control. Most of the time you add List Items dynamically. For Example:<\/strong> you have some list of Values stored in an Array Variable and you want all of them to Add in the ListBox. In the below Code, you will learn how to Add List Items from an Array Variable using For loop.<\/p>\n Take the Above Code and Paste it in a Regular Module and Run it after adding a Listbox in your Sheet. It will Add 10 Items in the ListBox from “List 1” to “List 10”.<\/strong> But there is a problem in Above code. If you run the same Code twice, in your ListBox, there will be 20 list get added, if you run thrice then 30 and so on. Why? because as i told earlier that .AddItem<\/strong> always append the list. It does not clear the Previous ones and then add the new one. To overcome this Problem, we need to put a Statement to Clear all the Items before you add list in the Drop Down list item. Therefore in the below code, this problem will not occur.<\/p>\n \nAs you saw how to Clear or Remove All the Items from the ListBox.<\/strong> Now you will learn how to remove a particular Item from ListBox<\/strong><\/p>\n If you want to Remove a particular Item from the ListBox then use the below code to remove item from the ListBox. For Removing an Item, you need to pass Index Number as an Input Parameter.<\/p>\n The Above Code will Always Remove the First List Item from the ListBox. As you can see by default no List value is selected after adding items. If you want to make Some List Item as Default One then follow the below. \n If you want to Select the Last Item of the ListBox By default, then use the below Code:<\/strong> <\/p>\n Below is the Simple Code which will show you how to get the Selected Value from the ListBox. .Value <\/strong> property of the Control ListBox1<\/strong> returns the Selected Value of the ListBox.<\/p>\n <\/p>\n .Value Property of the ListBox Control returns a Value, Only when the MultiSelect<\/strong> Property of the Control is Set to Single<\/strong>. If it is selected as Multiple<\/strong> then you need to Follow the below Code to get the List of All Selected Items from the ListBox.<\/p>\n <\/p>\n .ListCount<\/strong> is a Property in Of ListBox object to get the Total Number of Items in a ListBox. .ListCount<\/strong> always returns a Number. <\/p>\n
\n
\nIf you want to read more about Excel Macro… visit Excel Macro Tutorial<\/a><\/strong>
\n <\/p>\n
\n
\n1. Adding Items in Excel ListBox
\n2. Removing Items from ListBox
\n3. Removing All items from ListBox
\n4. Selecting List Item from ListBox
\n5. Making ListBox as Check Box List (How to Check more than one Items in List Box)
\n6. Making ListBox as Radio Button List in Excel (How to Check Only one Item in List Box)
\n7. Excel ListBox Default Value
\n8. Get the Total Count of Items in a List Box
\n
\n…..and many more<\/strong> things about Excel ListBox.
\n
\nFirst will learn how to place ListBox in your Excel Sheet.
\nBy going to Developer Tab –> Insert –> ListBox Activex Control<\/strong><\/p>\n
\nYou can Also re-size the Height and Width etc keeping it in Design Mode
\n
\nBasically there are two ways of Adding list Items in ListBox. 1. By Setting Range Formula in the Properties of ListBox.
\n
\n2. By using Excel VBA Code<\/strong>
\n<\/p>\n Add List Items in ListBox by Using Properties: <\/h3>\n
\n
\n Step 1.<\/strong> Select the ListBox Control and Right Click and Open the Properties Window
\n Step 2.<\/strong> Now Enter the Cell Range in ListFillRange<\/strong> Property as shown below:
\n
\n How to Make List of Check Boxes and Radio Buttons using this List Box <\/h3>\n
\nTo make this ListBox as a List of CheckBoxes, you need to change the below properties of the List Box:
\n1. ListStyle <\/strong> Property of the List Box to 0-fmListStyleOption<\/strong>.
\n2. MultiSelect Property<\/strong> of the ListBox set to 1 – fmMultiSelectMulti<\/strong><\/p>\n
\n1. ListStyle <\/strong> Property of the List Box to 0-fmListStyleOption<\/strong>.
\n2. MultiSelect Property<\/strong> of the ListBox set to 1 – fmMultiSelectSingle<\/strong><\/p>\n Add List Items in ListBox by Using Excel Macro (VBA Code): <\/h3>\n
\nTo Add Item List in Drop Down List, there is a very Simple VBA Syntax as shown Below:
\n
\n<ListBoxName>.AddItem <List Item> , <Index Number ><\/strong>
\n <\/p>\n\r\nSub Insert_Item_List()\r\n\r\nSheet1.ListBox1.AddItem \"List 1\", 0\r\nSheet1.ListBox1.AddItem \"List 2\", 1\r\nSheet1.ListBox1.AddItem \"List 3\", 2\r\nSheet1.ListBox1.AddItem \"List 4\", 3\r\nSheet1.ListBox1.AddItem \"List 5\", 4\r\n\r\nEnd Sub\r\n\r\n<\/code><\/pre>\n
\n
\nImportant:<\/strong>
\nIn ListBox, when you are adding a New Item, It always gets added at the End of the List Which are already assigned to that Drop Down List. It means, It always appends the items to the list. So Every time you want a fresh List of Items in your ListBox, then before adding any new Item, Clear all the Existing Items from the ListBox. Below is the Code How to Clear All the Items from the ListBox<\/strong>
\n <\/p>\n\n
\n \n
\nSheet1.ListBox1.Clear
\n \n<\/td>\n<\/tr>\n<\/table>\n\r\n\r\nSub Insert_Item_List()\r\n\r\nDim iCounter As Integer\r\nDim Item(10) As String\r\n'Store 10 List Items in an Array Variable\r\nFor i = 0 To 9\r\n Item(i) = \"Option \" & i + 1\r\nNext\r\n'Now Add all these items from Array Variable to ListBox\r\nFor i = 0 To 9\r\n Sheet1.ListBox1.AddItem Item(i)\r\nNext\r\nEnd Sub\r\n<\/code><\/pre>\n
\r\n\r\nSub Insert_Item_List()\r\n\r\nDim iCounter As Integer\r\nDim Item(10) As String\r\n'Store 10 List Items in an Array Variable\r\nFor i = 0 To 9\r\n Item(i) = \"Option \" & i + 1\r\nNext\r\n'Before Adding items to the List, Clear the List Box\r\n Sheet1.ListBox1.Clear\r\n'Now Add all these items from Array Variable to ListBox\r\nFor i = 0 To 9\r\n Sheet1.ListBox1.AddItem Item(i)\r\nNext\r\nEnd Sub\r\n\r\n<\/code><\/pre>\n
\n<ListBoxName>.RemoveItem <Index Number ><\/strong>
\n <\/p>\n\r\n\r\nSheet1.ListBox1.RemoveItem 0\r\n\r\n<\/code><\/pre>\n
\n<\/p>\n How to Select First List Item as by Default: <\/h3>\n
\n
\n<ListBoxName>.ListIndex= <Index Number ><\/strong>
\n
\nTo select an Item from the ListBox you need to use the below Code
\n <\/p>\n\r\n\r\nSheet1.ListBox1.ListIndex = 0\r\n\r\n<\/code><\/pre>\n
\nIn the Above example, First Item will be by default selected as I have used the Index Number as Zero (0). Similarly if you want to Select second or Third… give the Index Number of that Item.
\n
\n Want to Select Blank Option in ListBox: <\/strong> To Select Blank Option from the ListBox, you should use Index Number as -1<\/strong>.
\n <\/p>\n\r\n\r\nSheet1.ListBox1.ListIndex = -1\r\n\r\n<\/code><\/pre>\n
\n <\/p>\n\r\n\r\nSheet1.ListBox1.ListIndex = Sheet1.ListBox1.ListCount - 1\r\n\r\n<\/code><\/pre>\n
How to Get Selected Value of the ListBox: <\/h3>\n
\r\n\r\nMsgBox \"Selected Value is\" & ListBox1.Value\r\n\r\n<\/code><\/pre>\n
How to Get List of All Selected Items from the List Box? <\/h3>\n
\r\nSub GetSelectedItems()\r\nDim SelectedItemArray() As String\r\nReDim SelectedItemArray(ListBox1.ListCount) As String\r\nFor i = 0 To ListBox1.ListCount - 1\r\n If ListBox1.Selected(i) = True Then\r\n SelectedItemArray(i) = ListBox1.List(i)\r\n End If\r\nNext\r\nEnd Sub\r\n\r\n<\/code><\/pre>\n
How to Get Total Count of Items in a List Box<\/h3>\n
\nRefer the Below Code. It will give the the Total Number of Items in the ListBox1<\/strong>
\n<\/p>\n\r\nMsgBox (ListBox1.ListCount)\r\n<\/code><\/pre>\n