{"id":12160,"date":"2022-11-04T11:57:33","date_gmt":"2022-11-04T11:57:33","guid":{"rendered":"https:\/\/officetuts.net\/excel\/?p=12160"},"modified":"2024-03-30T11:53:14","modified_gmt":"2024-03-30T11:53:14","slug":"get-value-from-listbox-using-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/","title":{"rendered":"Get Value From Listbox Using VBA"},"content":{"rendered":"\n

With Excel and VBA especially, options for everyone are virtually limitless. In VBA, users can create Macros, UserForms, and ClassModules<\/strong>.<\/p>\n\n\n\n

One of the most used and convenient options with UserForms is ListBoxes<\/strong>, which is an integral part of it.<\/p>\n\n\n\n

In the example below, we will show how can get value from ListBox with VBA<\/strong>.<\/p>\n\n\n\n

Creating ListBox<\/h2>\n\n\n\n

First things first, our mission is to create the UserForm<\/strong>. To do so, we open up the VBA by clicking ALT + F11<\/strong> on our keyboard. In the Insert menu<\/strong>, or by right-clicking in the left window, we will choose UserForm<\/strong>:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

We need to add a Toolbox<\/strong> to manipulate with UserForm<\/strong>. To do so, we will click on the View tab<\/strong>, and then choose Toolbox<\/strong>:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

In the Toolbox<\/strong>, we will choose the ListBox option<\/strong>:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

And we will create it in the middle of our UserForm<\/strong>:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

We will now add the CommandButton<\/strong> from the Toolbox <\/strong>as well, to control the UserForm<\/strong>. We will then change the caption of UserForm<\/strong> to \u201cCountry\u201d<\/strong>, and the caption of CommandButton to \u201cSelect A Country\u201d<\/strong>:<\/p>\n\n\n\n

\"A<\/figure>\n\n\n\n

Create a List in Listbox<\/h2>\n\n\n\n

Now we need to add some code to our UserForm<\/strong>. At this moment, it is very important to remember that the ListBox is a part of UserForm<\/strong>, so we need to change the code in UserForm<\/strong>. We will double-click on it, and then insert the following code:<\/p>\n\n\n\n

Private Sub UserForm_Initialize()\n    'Creating and assigning the Array to the List Box\n    Dim mylist As Variant\n    mylist = Array(\"Argentina\", \"Brazil\", \"China\", \"Europe\", \"Russia\", \"USA\")\n    ListBox1.list = mylist\nEnd Sub<\/code><\/pre>\n\n\n\n

This code creates a variable mylist<\/strong> as a variant<\/strong> and then defines it as an array containing several countries. Then it defines that this variable will be equal to our ListBox<\/strong>, meaning that these countries will appear when we run the code. This is what the code looks like in the VBA:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

When we run this code by clicking F5<\/strong> while in the VBA, this is what we will end up with:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

Sadly, this code does not do anything with the UserForm<\/strong>, but rather just shows the list.<\/p>\n\n\n\n

Get Value From Listbox<\/h2>\n\n\n\n

We need additional code to get the value into the workbook. For this, we will first create the table with students and the country they are from<\/strong> (this field is to be populated):<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

This is the code that we need for this:<\/p>\n\n\n\n

Private Sub CommandButton1_Click()\n    Dim j As Long\n    'Loop through the whole list\n    For j = 0 To ListBox1.ListCount - 1\n        'Verify if anything was chosen\n        If ListBox1.Selected(j) Then\n            'Insert selected item in the sheet\n            Range(\"B\" & Rows.Count).End(xlUp).Offset(1).Value = ListBox1.list(j)\n        End If\n    Next j\nEnd Sub<\/code><\/pre>\n\n\n\n

This code is run when the command button is clicked. First, we define the \u201cj\u201d variable as long<\/strong>, then we use For Next Loop<\/strong> where we will define that the selected country ends up in column B<\/strong>, but only if a certain country from the list is selected.<\/p>\n\n\n\n

When we execute the code by pressing F5<\/strong> on the keyboard while in the VBA, we will get the same screen as before, but now, when we select any country on the list, it will be inserted in our table:<\/p>\n\n\n\n

\"Graphical<\/figure>\n","protected":false},"excerpt":{"rendered":"

With Excel and VBA especially, options for everyone are virtually limitless. In VBA, users can create Macros, UserForms, and ClassModules. One of the…<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[190],"yoast_head":"\nGet Value From Listbox Using VBA<\/title>\n<meta name=\"description\" content=\"Excel UserForm ListBox with VBA: Learn how to create a UserForm in Excel and use the ListBox option to create a convenient list.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get Value From Listbox Using VBA\" \/>\n<meta property=\"og:description\" content=\"Excel UserForm ListBox with VBA: Learn how to create a UserForm in Excel and use the ListBox option to create a convenient list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-04T11:57:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-30T11:53:14+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png\" \/>\n<meta name=\"author\" content=\"Harun Spahic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Harun Spahic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/\"},\"author\":{\"name\":\"Harun Spahic\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\"},\"headline\":\"Get Value From Listbox Using VBA\",\"datePublished\":\"2022-11-04T11:57:33+00:00\",\"dateModified\":\"2024-03-30T11:53:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/\"},\"wordCount\":454,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png\",\"keywords\":[\"pinterest\"],\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/\",\"name\":\"Get Value From Listbox Using VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png\",\"datePublished\":\"2022-11-04T11:57:33+00:00\",\"dateModified\":\"2024-03-30T11:53:14+00:00\",\"description\":\"Excel UserForm ListBox with VBA: Learn how to create a UserForm in Excel and use the ListBox option to create a convenient list.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get Value From Listbox Using VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\",\"name\":\"Harun Spahic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"caption\":\"Harun Spahic\"},\"sameAs\":[\"http:\/\/human.bsru.ac.th\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get Value From Listbox Using VBA","description":"Excel UserForm ListBox with VBA: Learn how to create a UserForm in Excel and use the ListBox option to create a convenient list.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/","og_locale":"en_US","og_type":"article","og_title":"Get Value From Listbox Using VBA","og_description":"Excel UserForm ListBox with VBA: Learn how to create a UserForm in Excel and use the ListBox option to create a convenient list.","og_url":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/","article_published_time":"2022-11-04T11:57:33+00:00","article_modified_time":"2024-03-30T11:53:14+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png"}],"author":"Harun Spahic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Harun Spahic","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/"},"author":{"name":"Harun Spahic","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2"},"headline":"Get Value From Listbox Using VBA","datePublished":"2022-11-04T11:57:33+00:00","dateModified":"2024-03-30T11:53:14+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/"},"wordCount":454,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png","keywords":["pinterest"],"articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/","url":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/","name":"Get Value From Listbox Using VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png","datePublished":"2022-11-04T11:57:33+00:00","dateModified":"2024-03-30T11:53:14+00:00","description":"Excel UserForm ListBox with VBA: Learn how to create a UserForm in Excel and use the ListBox option to create a convenient list.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/11\/graphical-user-interface-application-description-3.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/get-value-from-listbox-using-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Get Value From Listbox Using VBA"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."},{"@type":"Person","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2","name":"Harun Spahic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","caption":"Harun Spahic"},"sameAs":["http:\/\/human.bsru.ac.th\/"]}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/12160"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=12160"}],"version-history":[{"count":2,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/12160\/revisions"}],"predecessor-version":[{"id":14405,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/12160\/revisions\/14405"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=12160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=12160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=12160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}