Now Drag and Drop that Control any where you want in your Spreadsheet. \nYou can Also re-size the Height and Width etc keeping it in Design Mode \n
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. \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 To Add items by setting Properties of the ListBox, follow the below Steps: \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: \nExcel 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: \n
Excel Macro Tutorial – Add ListBox 2<\/p><\/div><\/p>\n
How to Make List of Check Boxes and Radio Buttons using this List Box <\/h3>\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> \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>\nExcel 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>\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 – fmMultiSelectSingle<\/strong><\/p>\nExcel Macro Tutorial – Add Check List Box<\/p><\/div>\n
Add List Items in ListBox by Using Excel Macro (VBA Code): <\/h3>\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. \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>\nIn the Above Code, Sheet1<\/strong> is the Sheet Name where ListBox is there and ListBox1<\/strong> is the Name of the ListBox Control. \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>\nMost 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\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>\nTake 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\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\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>\nIf 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
\n<ListBoxName>.RemoveItem <Index Number ><\/strong> \n <\/p>\n\r\n\r\nSheet1.ListBox1.RemoveItem 0\r\n\r\n<\/code><\/pre>\nThe Above Code will Always Remove the First List Item from the ListBox. \n<\/p>\n
How to Select First List Item as by Default: <\/h3>\n 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 \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 If you want to Select the Last Item of the ListBox By default, then use the below Code:<\/strong> \n <\/p>\n\r\n\r\nSheet1.ListBox1.ListIndex = Sheet1.ListBox1.ListCount - 1\r\n\r\n<\/code><\/pre>\n<\/p>\n
How to Get Selected Value of the ListBox: <\/h3>\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
\r\n\r\nMsgBox \"Selected Value is\" & ListBox1.Value\r\n\r\n<\/code><\/pre>\nHow to Get List of All Selected Items from the List Box? <\/h3>\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\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<\/p>\n
How to Get Total Count of Items in a List Box<\/h3>\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. \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<\/p>\n
\n\n\n \nTo Check out more Excel Macro Tutorials, visit Excel Macro Tutorial<\/a><\/strong> \n\n<\/td>\n<\/tr>\n<\/table>\n<\/span>","protected":false},"excerpt":{"rendered":"As we saw in the Previous Article you learnt How to use Drop Down Box in Excel If you want to read more about Excel Macro… visit Excel Macro Tutorial In this Article you will learn How to Use ListBox in Excel. It will include 1. Adding Items in Excel ListBox 2. […]<\/p>\n","protected":false},"author":45,"featured_media":242537,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1675],"tags":[],"class_list":["post-12132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-macro-for-beginners"],"yoast_head":"\n
Excel Macro Tutorial : How to Use ListBox in Excel - Let's excel in Excel<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n