Working with ListBox Control in Excel VBA

Working with ListBox Control in Excel VBA Hey guys welcome back to vba 80z my name is lumpamai and in today's video i'll be showing you how to create search autocomplete suggestions also known as predictive search this is a.

Very useful feature to include in your user interface which will definitely help improve your user experience let me give you a very quick demo.

This is the custom text field that we created from our first video it has a watermark it has an icon and then it has a custom border so once you click on this here.

And type in any client's name for an example let's say that i am looking for rebecca i'll say re and then it will auto suggest me all these client names.

For an example well let's say you type in my name or it could be any name starting with v wins it could be mary mary iron mary rose example so this this.

Is what we learned today if you missed our prior videos all these videos will be added to playlists called professional ui slash ux as always project file source codes used.

Working with ListBox Control in Excel VBA

Within tutorials will be made available absolutely at no cost so you can change them as per your requirement the only request from you is to support my channel so i can keep sharing all.

These videos and programs so before we begin please do not forget to subscribe to my channel and hit the bell icon for upcoming videos first let me take a few minutes.

To quickly show you how to use list box control in your user phone i'm gonna quickly insert a new user from here from the toolbox i'm gonna bring in the.

List box from the properties as usual you can see special effect the border the background color and so on for now i'm gonna make this to each and.

And here one thing to note here is once you add the list item here to your list box but there are multiple options you can make the user select multiple items single.

Item or extend it extended this basically the user can hold down the control key and select multiple items otherwise multi select.

    Will be like as soon as the user clicks

    On it it's going to get selected if there are multiple items within the user if i load up this form now it's blind add a quick um.

    Let me add a button here like this this is a demo and we'll say say me dot list box dot add whatever you add here is gonna be um added to the list box so let's say dummy.

    And then one so once i click on this one item is added right so this is very simple way to add let's say that you have to add multiple items.

    I'll i as long and then we'll loop one to ten maybe this is an example for now like this of one we'll say i so i will become our.

    Dynamic number there so before we add it's better to clear out so clear so this will clear out the list box whatever is there.

    Like this okay all 10 items so if i click on it it will clear and then re-add them like that okay so that is a simple way to add and let's say that you have to um.

    Return the value of what was selected so let's quickly check that as well because you might want to pull what was selected by the user okay so let's see that i have a box here this.

    Is an example a text box and whenever the user clicks on this list box item we want to return it into text box one okay.

    One dot value is equal to so we'll say um list box one dot list like this and then here you'll just specify uh since we are already just having.

    Uh one item we can just use this

    List index itself like this okay so it's very simple to use this feature but it's very useful at the same.

    Time so i hope that is clear now very quickly now going back to uh we'll change the property of the form now to multi-select if i.

    Now as soon as i select it gets selected okay now the other property here the last property that you have here is um extended property.

    So once i load this up and click on this you still select only one if you have to select multiple of them you have to hold down the control key from your keyboard and then.

    Click on it it will get selected let's say that you want to return all these selected items uh let me quickly show you that as well i'm just gonna add another button let me.

    Just leave this here for now i'm just going to replicate this list box something like that selected items something like that now let's.

    Write a quick loop to look through all the items within the listbox one and then we'll try to transfer whatever was selected into the list box too for an.

    Example so we'll say dim x as long and then for x equal to so the index starts from zero for the list box.

    Um we'll get the count minus one is just to take care because it's just starting from um zero that's why so now we'll say if me dot list box one down.

    Selected whatever is selected for that particular index so it's going to look true and then it's going to check if that particular index is selected.

    If it is true then we're going to bring that particular item to this box too we'll say need a listbox 2 dot add item and then here we'll say um.

    Need a list box sorry list box 1 dot list and then here was the index is going to be x and then we're going to say zero because we only.

    Have one column at the moment okay firing up this again this will add so like this then add here so whatever is selected here.

    You can see it has been brought to this this is how you can add and this is how you can read the data of whatever is selected let's say you want to bring in multiple.

    Columns from here uh we can do one thing let's say that we want to add a list like this to the list box three three columns like this okay so we'll just loop from.

    One to ten again and then we'll add this to the list this is an example me dot listbox one dot column column count is equal to uh the number of columns that you want.

    To add you can specify it here like this and then once you have this you can also specify um the the column width and then within this it will just take.

    Care by itself so you just don't have to worry too much for this like this with the comma you can specify the width of each of the column very nice.

    Now let's say here instead of dummy we'll load the data from um from our excel like this okay so since it's going to start from.

    Row number two till let's do it till serial number 10 here so till 11 we'll say this okay this is not x i just changed it from there okay so i'm gonna start it up.

    Okay this is not looking good i forgot to add other columns coming back here sorry 30 let's make this 70 each for another one.

    So here this is adding only one item and we're saying this is three right so i just forgot to mention this when you are adding multiple item you know the syntax.

    Will change slightly so we'll say add the item and then we'll say um me dot mid.listbox1 dot list and then here you'll specify the.

    Index that you want to add that is going to be i since i is starting from 2 we'll just do minus 2 because it's starting from zero okay and then the column number that you want.

    To add to the first will be zero and then it's it's going to be like let me quickly replicate this for three columns right this is for the row and this is for the column so i'll.

    Say one and then there's gonna be two like this similarly here this is your axle range the the index row of that we are looking through.

    The ir array and then the column similarly will do two and three let's try this so like this whatever is here is getting added here similarly like we did.

    Earlier um you can look through again and get the selected item but in this scenario like this it's gonna just get the first one if you want to bring in.

    Um additional columns uh then you'll have to specify what you want to get it from there but in this scenario since we already have only one particular.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=XjGQ06wyuHo
Previous Post Next Post