Excel VBA listbox selected item value
ListBox is one of the UserForm control. You can select and drag ListBox on the UserForm. This control is used to display list of items to a list. This is used on the UserForm. Please find more details about ListBox_Control in the following chapter. You can see how to load items to listbox_Control, how to move items from one listbox to another listbox, how to select items from a listbox_Control, etc.., Show
In this topic:
VBA ListBox_Control on the UserFormPlease find more details about VBA ActiveX ListBox_Control and how we are adding it on the UserForm.
Code: Private Sub UserForm_Initialize() ListBox1.AddItem "MBA" ListBox1.AddItem "MCA" ListBox1.AddItem "MSC" ListBox1.AddItem "MECS" ListBox1.AddItem "CA" End SubAdd Items to ListBox_Control using VBAPlease find the following code, it will show you how to add list items to list box. In the above code ListBox1 is the name of the listbox_Control. Where additem is the property of listbox. Clear Items from the ListBox using VBAPlease find the following code, it will show you how to clear the list box items. The below code clears the list box1 items on the UserForm1. Sub Clr_LstBx() UserForm3.ListBox1.Clear End SubCheck if a List box item is selected or not using VBAPlease find the below code to know how to check if a List box is selected or not using VBA. In the below example (0) is the index number. Sub Chk_Item_SelectOrNot() If UserForm3.ListBox1.Selected(0) = True Then MsgBox "First item has selected in the ListBox." Else MsgBox "First item has not selected in the ListBox." End If End SubVBA ListBox Default Values in ExcelHere is the VBA list box default values in Excel. After adding items to list box by using any of the below code you can define the default value. Code 1:The below code is useful to select blank option in list box. Where -1 is the index number. Sub LstBx_Dflt_Val_Ex1() UserForm3.ListBox1.ListIndex = -1 End SubCode 2:The below code is useful to select first item in the list box from the available list. . Where 0 is the index number. Sub LstBx_Dflt_Val_Ex2() UserForm3.ListBox1.ListIndex = 0 End SubCode 3:The below code is useful to select second item in the list box from the available list. Where 1 is the index number. Sub LstBx_Dflt_Val_Ex3() UserForm3.ListBox1.ListIndex = 1 End SubCode 4:The below code is useful to select the last item in the list box from the available list. Where 1 is the index number. Sub LstBx_Dflt_Val_Ex4() UserForm3.ListBox1.ListIndex = UserForm3.ListBox1.Count - 1 End SubGet the total count of Listbox ItemsHere is the following example, it will show you how to get the total count of items in a list box. In the below example ListBox1 is the list box name and ListCount is the property of list box. Sub Get_Ttl_Cnt() MsgBox "Total Items in a ListBox is " & UserForm3.ListBox1.ListCount End SubOutput: Move all Items from ListBox1 to ListBox2Please find the below example code, it shows how to Move all Items from ListBox1 to ListBox2. In the below example ListBox1 and ListBox2 are the list box names. Sub Move_ListBox_Items() 'Variable declaration Dim iCnt As Integer 'Moving ListBox1 Items to ListBox2 For iCnt = 0 To ListBox1.ListCount - 1 ListBox2.AddItem ListBox1.List(iCnt) Next iCnt 'Then Clear the ListBox1 Items ListBox1.Clear End SubGet Selected Items from ListBox1 to ListBox2Please find the below example code, it shows how to Get Selected Items from ListBox1 to ListBox2. In the below example ListBox1 and ListBox2 are the list box names. Sub Get_ListBox_Selected_Items() 'Variable declaration Dim iCnt As Integer 'Get Selcted Items from ListBox1 to ListBox2 For iCnt = 0 To ListBox1.ListCount - 1 'Check ListBox Item has selcted or not If ListBox1.Selected(iCnt) = True Then ListBox2.AddItem ListBox1.List(iCnt) End If Next End SubMake ListBox to Select Multiple ItemsPlease find the below example code, it shows how to make ListBox to Select Multiple Items. In the below example ListBox1 is the list box name. Sub Multiple_ListBox_Selection() ListBox1.MultiSelect = fmMultiSelectMulti End SubPopulate ListBox from an ArrayPlease find the below example code, it shows how to populate ListBox from an Array. In the below example arrList is the array name. And ListBox1 is the list box name. Sub Load_ListBox_From_Array() 'Load Items to an Array arrList = Array("Item 1", "Item 2", "Item 3") 'Load an Array Items to ListBox ListBox1.List = arrList End SubMore Details About the ListBox_ControlVBA ListBox Excel Macros Examples Codes Adding Clearing Multiple ItemsPlease find the following link for more details about VBA ListBox Excel Macros Examples and Codes Adding and Clearing Multiple Items. Read More VBA to Remove Duplicates in ListBox ExcelPlease find more details about Remove Duplicates in ListBox in Excel VBA. Read More Excel VBA FAQs: Frequently Asked QuestionsPlease find the most frequently asked questions and answers for your reference. These are explained more detailed way with examples. Premium Project Management Templates120+ Professional Project Management Templates!A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates. PREMIUM TEMPLATES LIMITED TIME OFFER ON SALE80% OFF BROWSE ALL TEMPLATES Advanced Project Planning Templates Excel Templates VIEW DETAILS 120+ Project Management Templates Pack Excel | PowerPoint | Word VIEW DETAILS ULTIMATE RESOURCE MANAGEMENT TEMPLATE Excel Template VIEW DETAILS 50+ Essential Project Management Templates Excel | PowerPoint | Word VIEW DETAILS Project Portfolio ManagementTemplates Excel | PowerPoint Templates VIEW DETAILS 50+ Excel Project Management Templates Excel Templates VIEW DETAILS Share This Story, Choose Your Platform! |